source: trunk/doc/src/tutorials/widgets-tutorial.qdoc@ 560

Last change on this file since 560 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \page widgets-tutorial.html
44
45 \title Widgets Tutorial
46 \ingroup tutorials
47
48 \brief This tutorial covers basic usage of widgets and layouts, showing how they are used to build GUI applications.
49
50 \section1 Introduction
51
52 Widgets are the basic building blocks of graphical user interface (GUI)
53 applications made with Qt. Each GUI component, such as a button, label or
54 text editor, is a widget and can be placed within an existing user
55 interface or displayed as an independent window. Each type of component
56 is provided by a particular subclass of QWidget, which is itself a
57 subclass of QObject.
58
59 QWidget is not an abstract class; it can be used as a container for other
60 widgets, and can be subclassed with minimal effort to create custom
61 widgets. It is most often used to create windows in which other widgets
62 are placed.
63
64 As with \l{QObject}s, widgets can be created with parent objects to
65 indicate ownership, ensuring that objects are deleted when they are no
66 longer used. With widgets, these parent-child relationships have an
67 additional meaning: each child is displayed within the screen area
68 occupied by its parent. This means that, when a window is deleted, all
69 the widgets it contains are automatically deleted.
70
71 \section1 Creating a Window
72
73 If a widget is created without a parent, it is treated as a window, or
74 \e{top-level widget}, when it is shown. Since it has no parent object to
75 ensure that it is deleted when no longer needed, it is up to the
76 developer to keep track of the top-level widgets in an application.
77
78 In the following example, we use QWidget to create and show a window with
79 a default size:
80
81 \raw HTML
82 <table align="left" width="100%">
83 <tr class="qt-code"><td>
84 \endraw
85 \snippet snippets/widgets-tutorial/toplevel/main.cpp create, resize and show
86 \raw HTML
87 </td><td align="right">
88 \endraw
89 \inlineimage widgets-tutorial-toplevel.png
90 \raw HTML
91 </td></tr>
92 </table>
93 \endraw
94
95 We can add a child widget to this window by passing \c window as the
96 parent to its constructor. In this case, we add a button to the window
97 and place it in a specific location:
98
99 \raw HTML
100 <table align="left" width="100%">
101 <tr class="qt-code"><td>
102 \endraw
103 \snippet snippets/widgets-tutorial/childwidget/main.cpp create, position and show
104 \raw HTML
105 </td><td align="right">
106 \endraw
107 \inlineimage widgets-tutorial-childwidget.png
108 \raw HTML
109 </td></tr>
110 </table>
111 \endraw
112
113 The button is now a child of the window and will be deleted when the
114 window is destroyed. Note that hiding or closing the window does not
115 automatically destroy it.
116
117 \section1 Using Layouts
118
119 Usually, child widgets are arranged inside a window using layout objects
120 rather than by specifying positions and sizes explicitly. Here, we
121 construct a label and line edit widget that we would like to arrange
122 side-by-side.
123
124 \raw HTML
125 <table align="left" width="100%">
126 <tr class="qt-code"><td>
127 \endraw
128 \snippet snippets/widgets-tutorial/windowlayout/main.cpp create, lay out widgets and show
129 \raw HTML
130 </td><td align="right">
131 \endraw
132 \inlineimage widgets-tutorial-windowlayout.png
133 \raw HTML
134 </td></tr>
135 </table>
136 \endraw
137
138 The \c layout object we construct manages the positions and sizes of
139 widgets supplied to it with the \l{QHBoxLayout::}{addWidget()} function.
140 The layout itself is supplied to the window itself in the call to
141 \l{QWidget::}{setLayout()}. Layouts are only visible through the effects
142 they have on the widgets (and other layouts) they are responsible for
143 managing.
144
145 In the example above, the ownership of each widget is not immediately
146 clear. Since we construct the widgets and the layout without parent
147 objects, we would expect to see an empty window and two separate windows
148 containing a label and a line edit. However, when we tell the layout to
149 manage the label and line edit and set the layout on the window, both the
150 widgets and the layout itself are ''reparented'' to become children of
151 the window.
152
153 Just as widgets can contain other widgets, layouts can be used to provide
154 different levels of grouping for widgets. Here, we want to display a
155 label alongside a line edit at the top of a window, above a table view
156 showing the results of a query.
157
158 \raw HTML
159 <table align="left" width="100%">
160 <tr class="qt-code"><td>
161 \endraw
162 \snippet snippets/widgets-tutorial/nestedlayouts/main.cpp create, lay out widgets and show
163 \raw HTML
164 </td><td align="right">
165 \endraw
166 \inlineimage widgets-tutorial-nestedlayouts.png
167 \raw HTML
168 </td></tr>
169 </table>
170 \endraw
171
172 As well as QHBoxLayout and QVBoxLayout, Qt also provides QGridLayout
173 and QFormLayout classes to help with more complex user interfaces.
174*/
Note: See TracBrowser for help on using the repository browser.