File: widgets-tutorial.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (110 lines) | stat: -rw-r--r-- 7,256 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- widgets-tutorial.qdoc -->
  <title>Qt 4.8: Widgets Tutorial</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
<li>Widgets Tutorial</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#introduction">Introduction</a></li>
<li class="level1"><a href="#writing-a-main-function">Writing a main Function</a></li>
<li class="level1"><a href="#simple-widget-examples">Simple widget examples</a></li>
<li class="level1"><a href="#real-world-widget-examples">Real world widget examples</a></li>
<li class="level1"><a href="#building-the-examples">Building The Examples</a></li>
</ul>
</div>
<h1 class="title">Widgets Tutorial</h1>
<span class="subtitle"></span>
<!-- $$$widgets-tutorial.html-description -->
<div class="descr"> <a name="details"></a>
<a name="introduction"></a>
<h2>Introduction</h2>
<p>Widgets are the basic building blocks for graphical user interface (GUI) applications built with Qt. Each GUI component (e.g&#x2e; buttons, labels, text editor) is a <a href="qwidget.html">widget</a> that is placed somewhere within a user interface window, or is displayed as an independent window. Each type of widge is provided by a subclass of <a href="qwidget.html">QWidget</a>, which is itself a subclass of <a href="qobject.html">QObject</a>.</p>
<p><a href="qwidget.html">QWidget</a> is not an abstract class. It can be used as a container for other widgets, and it can be subclassed with minimal effort to create new, custom widgets. <a href="qwidget.html">QWidget</a> is often used to create a window inside which other <a href="qwidget.html">QWidget</a>s are placed.</p>
<p>As with <a href="qobject.html">QObject</a>s, <a href="qwidget.html">QWidget</a>s can be created with parent objects to indicate ownership, ensuring that objects are deleted when they are no longer used. With widgets, these parent-child relationships have an additional meaning: Each child widget is displayed within the screen area occupied by its parent widget. This means that when you delete a window widget, all the child widgets it contains are also deleted.</p>
<a name="writing-a-main-function"></a>
<h2>Writing a main Function</h2>
<p>Many of the GUI examples provided with Qt follow the pattern of having a <tt>main.cpp</tt> file, which contains the standard code to initialize the application, plus any number of other source/header files that contain the application logic and custom GUI components.</p>
<p>A typical <tt>main()</tt> function in <tt>main.cpp</tt> looks like this:</p>
<pre class="cpp"> <span class="preprocessor">#include &lt;QtGui&gt;</span>

 <span class="comment">// Include header files for application components.</span>
 <span class="comment">// ...</span>

 <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
 {
     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);

     <span class="comment">// Set up and show widgets.</span>
     <span class="comment">// ...</span>

     <span class="keyword">return</span> app<span class="operator">.</span>exec();
 }</pre>
<p>First, a <a href="qapplication.html">QApplication</a> object is constructed, which can be configured with arguments passed in from the command line. After the widgets have been created and shown, <a href="qapplication.html#exec">QApplication::exec</a>() is called to start Qt's event loop. Control passes to Qt until this function returns. Finally, <tt>main()</tt> returns the value returned by <a href="qapplication.html#exec">QApplication::exec</a>().</p>
<a name="simple-widget-examples"></a>
<h2>Simple widget examples</h2>
<p>Each of theses simple widget examples is written entirely within the <tt>main()</tt> function.</p>
<ul>
<li><a href="tutorials-widgets-toplevel.html">Creating a window</a></li>
<li><a href="tutorials-widgets-childwidget.html">Creating child widgets</a></li>
<li><a href="tutorials-widgets-windowlayout.html">Using layouts</a></li>
<li><a href="tutorials-widgets-nestedlayouts.html">Nested layouts</a></li>
</ul>
<a name="real-world-widget-examples"></a>
<h2>Real world widget examples</h2>
<p>In these <a href="examples-widgets.html">more advanced examples</a>, the code that creates the widgets and layouts is stored in other files. For example, the GUI for a main window may be created in the constructor of a <a href="qmainwindow.html">QMainWindow</a> subclass.</p>
<a name="building-the-examples"></a>
<h2>Building The Examples</h2>
<p>If you installed a binary package to get Qt, or if you compiled Qt yourself, the examples described in this tutorial should already be built and ready to run. If you wish to modify and recompile them, follow these steps:</p>
<ol class="1">
<li>From a command prompt, enter the directory containing the example you have modified.</li>
<li>Type <tt>qmake</tt> and press <b>Return</b>. If this doesn't work, make sure that the executable is on your path, or enter its full location.</li>
<li>On Linux/Unix and Mac OS X, type <tt>make</tt> and press <b>Return</b>; on Windows with Visual Studio, type <tt>nmake</tt> and press <b>Return</b>.</li>
</ol>
<p>An executable file is created in the current directory. On Windows, this file may be located in a <tt>debug</tt> or <tt>release</tt> subdirectory. You can run this executable to see the example code at work.</p>
</div>
<!-- @@@widgets-tutorial.html -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>