1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \page widgets-tutorial.html
|
---|
30 | \title Widgets Tutorial
|
---|
31 | \brief This tutorial covers basic usage of widgets and layouts, showing how
|
---|
32 | they are used to build GUI applications.
|
---|
33 |
|
---|
34 | \section1 Introduction
|
---|
35 |
|
---|
36 | Widgets are the basic building blocks for graphical user interface
|
---|
37 | (GUI) applications built with Qt. Each GUI component (e.g.
|
---|
38 | buttons, labels, text editor) is a \l{QWidget}{widget} that is
|
---|
39 | placed somewhere within a user interface window, or is displayed
|
---|
40 | as an independent window. Each type of widge is provided by a
|
---|
41 | subclass of QWidget, which is itself a subclass of QObject.
|
---|
42 |
|
---|
43 | QWidget is not an abstract class. It can be used as a container
|
---|
44 | for other widgets, and it can be subclassed with minimal effort to
|
---|
45 | create new, custom widgets. QWidget is often used to create a
|
---|
46 | window inside which other \l{QWidget}s are placed.
|
---|
47 |
|
---|
48 | As with \l{QObject}s, \l{QWidget}s can be created with parent
|
---|
49 | objects to indicate ownership, ensuring that objects are deleted
|
---|
50 | when they are no longer used. With widgets, these parent-child
|
---|
51 | relationships have an additional meaning: Each child widget is
|
---|
52 | displayed within the screen area occupied by its parent widget.
|
---|
53 | This means that when you delete a window widget, all the child
|
---|
54 | widgets it contains are also deleted.
|
---|
55 |
|
---|
56 | \section1 Writing a main Function
|
---|
57 |
|
---|
58 | Many of the GUI examples provided with Qt follow the pattern of
|
---|
59 | having a \c{main.cpp} file, which contains the standard code to
|
---|
60 | initialize the application, plus any number of other source/header
|
---|
61 | files that contain the application logic and custom GUI components.
|
---|
62 |
|
---|
63 | A typical \c main() function in \c{main.cpp} looks like this:
|
---|
64 |
|
---|
65 | \snippet doc/src/snippets/widgets-tutorial/template.cpp main.cpp body
|
---|
66 |
|
---|
67 | First, a QApplication object is constructed, which can be
|
---|
68 | configured with arguments passed in from the command line. After
|
---|
69 | the widgets have been created and shown, QApplication::exec() is
|
---|
70 | called to start Qt's event loop. Control passes to Qt until this
|
---|
71 | function returns. Finally, \c{main()} returns the value returned
|
---|
72 | by QApplication::exec().
|
---|
73 |
|
---|
74 | \section1 Simple widget examples
|
---|
75 |
|
---|
76 | Each of theses simple widget examples is written entirely within
|
---|
77 | the \c main() function.
|
---|
78 |
|
---|
79 | \list
|
---|
80 | \o \l {tutorials/widgets/toplevel} {Creating a window}
|
---|
81 |
|
---|
82 | \o \l {tutorials/widgets/childwidget} {Creating child widgets}
|
---|
83 |
|
---|
84 | \o \l {tutorials/widgets/windowlayout} {Using layouts}
|
---|
85 |
|
---|
86 | \o \l {tutorials/widgets/nestedlayouts} {Nested layouts}
|
---|
87 | \endlist
|
---|
88 |
|
---|
89 | \section1 Real world widget examples
|
---|
90 |
|
---|
91 | In these \l{Widget examples} {more advanced examples}, the code
|
---|
92 | that creates the widgets and layouts is stored in other files. For
|
---|
93 | example, the GUI for a main window may be created in the
|
---|
94 | constructor of a QMainWindow subclass.
|
---|
95 |
|
---|
96 | \section1 Building The Examples
|
---|
97 |
|
---|
98 | If you installed a binary package to get Qt, or if you compiled Qt
|
---|
99 | yourself, the examples described in this tutorial should already
|
---|
100 | be built and ready to run. If you wish to modify and recompile
|
---|
101 | them, follow these steps:
|
---|
102 |
|
---|
103 | \list 1
|
---|
104 |
|
---|
105 | \o From a command prompt, enter the directory containing the
|
---|
106 | example you have modified.
|
---|
107 |
|
---|
108 | \o Type \c qmake and press \key{Return}. If this doesn't work,
|
---|
109 | make sure that the executable is on your path, or enter its
|
---|
110 | full location.
|
---|
111 |
|
---|
112 | \o On Linux/Unix and Mac OS X, type \c make and press
|
---|
113 | \key{Return}; on Windows with Visual Studio, type \c nmake and
|
---|
114 | press \key{Return}.
|
---|
115 |
|
---|
116 | \endlist
|
---|
117 |
|
---|
118 | An executable file is created in the current directory. On
|
---|
119 | Windows, this file may be located in a \c debug or \c release
|
---|
120 | subdirectory. You can run this executable to see the example code
|
---|
121 | at work.
|
---|
122 | */
|
---|
123 |
|
---|
124 | /*!
|
---|
125 | \example tutorials/widgets/toplevel
|
---|
126 | \title Widgets Tutorial - Creating a Window
|
---|
127 |
|
---|
128 | If a widget is created without a parent, it is treated as a window, or
|
---|
129 | \e{top-level widget}, when it is shown. Since it has no parent object to
|
---|
130 | ensure that it is deleted when no longer needed, it is up to the
|
---|
131 | developer to keep track of the top-level widgets in an application.
|
---|
132 |
|
---|
133 | In the following example, we use QWidget to create and show a window with
|
---|
134 | a default size:
|
---|
135 |
|
---|
136 | \raw HTML
|
---|
137 | <table align="left" width="100%">
|
---|
138 | <tr class="qt-code"><td>
|
---|
139 | \endraw
|
---|
140 | \snippet tutorials/widgets/toplevel/main.cpp main program
|
---|
141 | \raw HTML
|
---|
142 | </td><td align="right">
|
---|
143 | \endraw
|
---|
144 | \inlineimage widgets-tutorial-toplevel.png
|
---|
145 | \raw HTML
|
---|
146 | </td></tr>
|
---|
147 | </table>
|
---|
148 | \endraw
|
---|
149 |
|
---|
150 | To create a real GUI, we need to place widgets inside the window. To do
|
---|
151 | this, we pass a QWidget instance to a widget's constructor, as we will
|
---|
152 | demonstrate in the next part of this tutorial.
|
---|
153 |
|
---|
154 | */
|
---|
155 |
|
---|
156 | /*!
|
---|
157 | \example tutorials/widgets/childwidget
|
---|
158 | \title Widgets Tutorial - Child Widgets
|
---|
159 |
|
---|
160 | We can add a child widget to the window created in the previous example by
|
---|
161 | passing \c window as the parent to its constructor. In this case, we add a
|
---|
162 | button to the window and place it in a specific location:
|
---|
163 |
|
---|
164 | \raw HTML
|
---|
165 | <table align="left" width="100%">
|
---|
166 | <tr class="qt-code"><td>
|
---|
167 | \endraw
|
---|
168 | \snippet tutorials/widgets/childwidget/main.cpp main program
|
---|
169 | \raw HTML
|
---|
170 | </td><td align="right">
|
---|
171 | \endraw
|
---|
172 | \inlineimage widgets-tutorial-childwidget.png
|
---|
173 | \raw HTML
|
---|
174 | </td></tr>
|
---|
175 | </table>
|
---|
176 | \endraw
|
---|
177 |
|
---|
178 | The button is now a child of the window and will be deleted when the
|
---|
179 | window is destroyed. Note that hiding or closing the window does not
|
---|
180 | automatically destroy it. It will be destroyed when the example exits.
|
---|
181 | */
|
---|
182 |
|
---|
183 | /*!
|
---|
184 | \example tutorials/widgets/windowlayout
|
---|
185 | \title Widgets Tutorial - Using Layouts
|
---|
186 |
|
---|
187 | Usually, child widgets are arranged inside a window using layout objects
|
---|
188 | rather than by specifying positions and sizes explicitly. Here, we
|
---|
189 | construct a label and line edit widget that we would like to arrange
|
---|
190 | side-by-side.
|
---|
191 |
|
---|
192 | \raw HTML
|
---|
193 | <table align="left" width="100%">
|
---|
194 | <tr class="qt-code"><td>
|
---|
195 | \endraw
|
---|
196 | \snippet tutorials/widgets/windowlayout/main.cpp main program
|
---|
197 | \raw HTML
|
---|
198 | </td><td align="right">
|
---|
199 | \endraw
|
---|
200 | \inlineimage widgets-tutorial-windowlayout.png
|
---|
201 | \raw HTML
|
---|
202 | </td></tr>
|
---|
203 | </table>
|
---|
204 | \endraw
|
---|
205 |
|
---|
206 | The \c layout object we construct manages the positions and sizes of
|
---|
207 | widgets supplied to it with the \l{QHBoxLayout::}{addWidget()} function.
|
---|
208 | The layout itself is supplied to the window itself in the call to
|
---|
209 | \l{QWidget::}{setLayout()}. Layouts are only visible through the effects
|
---|
210 | they have on the widgets (and other layouts) they are responsible for
|
---|
211 | managing.
|
---|
212 |
|
---|
213 | In the example above, the ownership of each widget is not immediately
|
---|
214 | clear. Since we construct the widgets and the layout without parent
|
---|
215 | objects, we would expect to see an empty window and two separate windows
|
---|
216 | containing a label and a line edit. However, when we tell the layout to
|
---|
217 | manage the label and line edit and set the layout on the window, both the
|
---|
218 | widgets and the layout itself are ''reparented'' to become children of
|
---|
219 | the window.
|
---|
220 | */
|
---|
221 |
|
---|
222 | /*!
|
---|
223 | \example tutorials/widgets/nestedlayouts
|
---|
224 | \title Widgets Tutorial - Nested Layouts
|
---|
225 |
|
---|
226 | Just as widgets can contain other widgets, layouts can be used to provide
|
---|
227 | different levels of grouping for widgets. Here, we want to display a
|
---|
228 | label alongside a line edit at the top of a window, above a table view
|
---|
229 | showing the results of a query.
|
---|
230 |
|
---|
231 | We achieve this by creating two layouts: \c{queryLayout} is a QHBoxLayout
|
---|
232 | that contains QLabel and QLineEdit widgets placed side-by-side;
|
---|
233 | \c{mainLayout} is a QVBoxLayout that contains \c{queryLayout} and a
|
---|
234 | QTableView arranged vertically.
|
---|
235 |
|
---|
236 | \raw HTML
|
---|
237 | <table align="left" width="100%">
|
---|
238 | <tr class="qt-code"><td>
|
---|
239 | \endraw
|
---|
240 | \snippet tutorials/widgets/nestedlayouts/main.cpp first part
|
---|
241 | \snippet tutorials/widgets/nestedlayouts/main.cpp last part
|
---|
242 | \raw HTML
|
---|
243 | </td><td align="right">
|
---|
244 | \endraw
|
---|
245 | \inlineimage widgets-tutorial-nestedlayouts.png
|
---|
246 | \raw HTML
|
---|
247 | </td></tr>
|
---|
248 | </table>
|
---|
249 | \endraw
|
---|
250 |
|
---|
251 | Note that we call the \c{mainLayout}'s \l{QBoxLayout::}{addLayout()}
|
---|
252 | function to insert the \c{queryLayout} above the \c{resultView} table.
|
---|
253 |
|
---|
254 | We have omitted the code that sets up the model containing the data shown
|
---|
255 | by the QTableView widget, \c resultView. For completeness, we show this below.
|
---|
256 |
|
---|
257 | As well as QHBoxLayout and QVBoxLayout, Qt also provides QGridLayout
|
---|
258 | and QFormLayout classes to help with more complex user interfaces.
|
---|
259 | These can be seen if you run \l{Qt Designer}.
|
---|
260 |
|
---|
261 | \section1 Setting up the Model
|
---|
262 |
|
---|
263 | In the code above, we did not show where the table's data came from
|
---|
264 | because we wanted to concentrate on the use of layouts. Here, we see
|
---|
265 | that the model holds a number of items corresponding to rows, each of
|
---|
266 | which is set up to contain data for two columns.
|
---|
267 |
|
---|
268 | \snippet tutorials/widgets/nestedlayouts/main.cpp set up the model
|
---|
269 |
|
---|
270 | The use of models and views is covered in the
|
---|
271 | \l{Item Views Examples} and in the \l{Model/View Programming} overview.
|
---|
272 | */
|
---|