0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the documentation of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
/*!
|
|
43 |
\group mainwindow-classes
|
|
44 |
\title Main Window and Related Classes
|
|
45 |
*/
|
|
46 |
|
|
47 |
/*!
|
|
48 |
\page application-windows.html
|
|
49 |
\title Application Windows and Dialogs
|
|
50 |
\ingroup frameworks-technologies
|
|
51 |
|
|
52 |
\nextpage The Application Main Window
|
|
53 |
|
|
54 |
A \l{Widgets}{widget} that is not embedded in a parent widget is called a window.
|
|
55 |
Usually, windows have a frame and a title bar, although it is also possible to create
|
|
56 |
windows without such decoration using suitable window flags). In Qt, QMainWindow
|
|
57 |
and the various subclasses of QDialog are the most common window types.
|
|
58 |
|
|
59 |
In applications, windows provide the screen space upon which the user
|
|
60 |
interface is built. Windows separate applications visually from each other
|
|
61 |
and usually provide a window decoration that allows the user to resize and
|
|
62 |
position the applications according to his preferences. Windows are typically
|
|
63 |
integrated into the desktop environment and to some degree managed by the
|
|
64 |
window management system that the desktop environment provides. For instance,
|
|
65 |
selected windows of an application are represented in the task bar.
|
|
66 |
|
|
67 |
\section1 Primary and Secondary Windows
|
|
68 |
|
|
69 |
Any QWidget that has no parent will become a window, and will on most platforms
|
|
70 |
be listed in the desktop's task bar. This is usually only wanted for one
|
|
71 |
window in the application, the \e{primary window}.
|
|
72 |
|
|
73 |
In addition, a QWidget that has a parent can become a window by setting the
|
|
74 |
\l{Qt::WindowType}{Qt::WA_Window} flag. Depending on the window management system
|
|
75 |
such \e{secondary windows} are usually stacked on top of their respective parent
|
|
76 |
window, and not have a task bar entry of their own.
|
|
77 |
|
|
78 |
The QMainWindow and the QDialog classes set the Qt::WA_Window flag in their
|
|
79 |
constructor, as they are designed to be used as windows and provide facilities
|
|
80 |
that are not wanted for child widgets.
|
|
81 |
|
|
82 |
\section1 Main Windows and Dialogs
|
|
83 |
|
|
84 |
\l{The Application Main Window} provides the framework for building the
|
|
85 |
application's main user interface, and are created by subclassing QMainWindow.
|
|
86 |
QMainWindow has its own layout to which you can add a \l{QMenuBar}{menu bar},
|
|
87 |
\l{QToolBar}{tool bars}, \l{QDockWidget}{dockable widgets} and a
|
|
88 |
\l{QStatusBar}{status bar}. The center area can be occupied by any kind of
|
|
89 |
QWidget.
|
|
90 |
|
|
91 |
\l{Dialog Windows} are used as secondary windows that present the user with
|
|
92 |
options and choices. Dialogs are created by subclassing QDialog and using
|
|
93 |
\l{Widgets and Layouts}{widgets and layouts} to implement the user interface.
|
|
94 |
In addition, Qt provides a number of ready-made standard dialogs that can be
|
|
95 |
used for standard tasks like file or font selection.
|
|
96 |
|
|
97 |
Both main windows and dialogs can be created with \QD, Qt's visual design tool.
|
|
98 |
Using \QD is a lot faster than hand-coding, and makes it easy to test different
|
|
99 |
design ideas. Creating designs visually and reading the code generated by
|
|
100 |
\l{uic} is a great way to learn Qt!
|
|
101 |
|
|
102 |
\keyword window geometry
|
|
103 |
\section1 Window Geometry
|
|
104 |
|
|
105 |
QWidget provides several functions that deal with a widget's
|
|
106 |
geometry. Some of these functions operate on the pure client area
|
|
107 |
(i.e. the window excluding the window frame), others include the
|
|
108 |
window frame. The differentiation is done in a way that covers the
|
|
109 |
most common usage transparently.
|
|
110 |
|
|
111 |
\list
|
|
112 |
\o \bold{Including the window frame:}
|
|
113 |
\l{QWidget::x()}{x()},
|
|
114 |
\l{QWidget::y()}{y()},
|
|
115 |
\l{QWidget::frameGeometry()}{frameGeometry()},
|
|
116 |
\l{QWidget::pos()}{pos()}, and
|
|
117 |
\l{QWidget::move()}{move()}.
|
|
118 |
\o \bold{Excluding the window frame:}
|
|
119 |
\l{QWidget::geometry()}{geometry()},
|
|
120 |
\l{QWidget::width()}{width()},
|
|
121 |
\l{QWidget::height()}{height()},
|
|
122 |
\l{QWidget::rect()}{rect()}, and
|
|
123 |
\l{QWidget::size()}{size()}.
|
|
124 |
\endlist
|
|
125 |
|
|
126 |
Note that the distinction only matters for decorated top-level
|
|
127 |
widgets. For all child widgets, the frame geometry is equal to the
|
|
128 |
widget's client geometry.
|
|
129 |
|
|
130 |
This diagram shows most of the functions in use:
|
|
131 |
\img geometry.png Geometry diagram
|
|
132 |
|
|
133 |
\section2 X11 Peculiarities
|
|
134 |
|
|
135 |
On X11, a window does not have a frame until the window manager
|
|
136 |
decorates it. This happens asynchronously at some point in time
|
|
137 |
after calling QWidget::show() and the first paint event the
|
|
138 |
window receives, or it does not happen at all. Bear in mind that
|
|
139 |
X11 is policy-free (others call it flexible). Thus you cannot
|
|
140 |
make any safe assumption about the decoration frame your window
|
|
141 |
will get. Basic rule: There's always one user who uses a window
|
|
142 |
manager that breaks your assumption, and who will complain to
|
|
143 |
you.
|
|
144 |
|
|
145 |
Furthermore, a toolkit cannot simply place windows on the screen. All
|
|
146 |
Qt can do is to send certain hints to the window manager. The window
|
|
147 |
manager, a separate process, may either obey, ignore or misunderstand
|
|
148 |
them. Due to the partially unclear Inter-Client Communication
|
|
149 |
Conventions Manual (ICCCM), window placement is handled quite
|
|
150 |
differently in existing window managers.
|
|
151 |
|
|
152 |
X11 provides no standard or easy way to get the frame geometry
|
|
153 |
once the window is decorated. Qt solves this problem with nifty
|
|
154 |
heuristics and clever code that works on a wide range of window
|
|
155 |
managers that exist today. Don't be surprised if you find one
|
|
156 |
where QWidget::frameGeometry() returns wrong results though.
|
|
157 |
|
|
158 |
Nor does X11 provide a way to maximize a window.
|
|
159 |
QWidget::showMaximized() has to emulate the feature. Its result
|
|
160 |
depends on the result of QWidget::frameGeometry() and the
|
|
161 |
capability of the window manager to do proper window placement,
|
|
162 |
neither of which can be guaranteed.
|
|
163 |
*/
|
|
164 |
|
|
165 |
/*!
|
|
166 |
\page mainwindow.html
|
|
167 |
\title The Application Main Window
|
|
168 |
\brief Everything you need for a typical modern main application window,
|
|
169 |
including menus, toolbars, workspace, etc.
|
|
170 |
|
|
171 |
\contentspage Application Windows and Dialogs
|
|
172 |
\nextpage Dialog Windows
|
|
173 |
|
|
174 |
\tableofcontents
|
|
175 |
|
|
176 |
\section1 Overview of the Main Window Classes
|
|
177 |
|
|
178 |
These classes provide everything you need for a typical modern main
|
|
179 |
application window, like the main window itself, menu and tool bars,
|
|
180 |
a status bar, etc.
|
|
181 |
|
|
182 |
\annotatedlist mainwindow-classes
|
|
183 |
|
|
184 |
\section1 The Main Window Classes
|
|
185 |
|
|
186 |
Qt 4 provides the following classes for managing main windows and
|
|
187 |
associated user interface components:
|
|
188 |
|
|
189 |
\list
|
|
190 |
\o QMainWindow remains the central class around which applications
|
|
191 |
can be built. The interface to this class has been simplified, and
|
|
192 |
much of the functionality previously included in this class is now
|
|
193 |
present in the companion QDockWidget and QToolBar classes.
|
|
194 |
|
|
195 |
\o QDockWidget provides a widget that can be used to create
|
|
196 |
detachable tool palettes or helper windows. Dock widgets keep track
|
|
197 |
of their own properties, and they can be moved, closed, and floated
|
|
198 |
as external windows.
|
|
199 |
|
|
200 |
\o QToolBar provides a generic toolbar widget that can hold a
|
|
201 |
number of different action-related widgets, such as buttons,
|
|
202 |
drop-down menus, comboboxes, and spin boxes. The emphasis on a
|
|
203 |
unified action model in Qt 4 means that toolbars cooperate well
|
|
204 |
with menus and keyboard shortcuts.
|
|
205 |
\endlist
|
|
206 |
|
|
207 |
\section1 Example Code
|
|
208 |
|
|
209 |
Using QMainWindow is straightforward. Generally, we subclass
|
|
210 |
QMainWindow and set up menus, toolbars, and dock widgets inside
|
|
211 |
the QMainWindow constructor.
|
|
212 |
|
|
213 |
To add a menu bar to the main window, we simply create the menus, and
|
|
214 |
add them to the main window's menu bar. Note that the
|
|
215 |
QMainWindow::menuBar() function will automatically create the menu bar
|
|
216 |
the first time it is called. You can also call
|
|
217 |
QMainWindow::setMenuBar() to use a custom menu bar in the main window.
|
|
218 |
|
|
219 |
\snippet doc/src/snippets/code/doc_src_qt4-mainwindow.qdoc 0
|
|
220 |
\dots
|
|
221 |
\snippet examples/mainwindows/menus/mainwindow.cpp 5
|
|
222 |
\dots
|
|
223 |
|
|
224 |
Once actions have been created, we can add them to the main window
|
|
225 |
components. To begin with, we add them to the pop-up menus:
|
|
226 |
|
|
227 |
\snippet examples/mainwindows/menus/mainwindow.cpp 10
|
|
228 |
\dots
|
|
229 |
\snippet examples/mainwindows/menus/mainwindow.cpp 11
|
|
230 |
\dots
|
|
231 |
|
|
232 |
The QToolBar and QMenu classes use Qt's action system to provide a
|
|
233 |
consistent API. In the above code, some existing actions were added to
|
|
234 |
the file menu with the QMenu::addAction() function. QToolBar also
|
|
235 |
provides this function, making it easy to reuse actions in different
|
|
236 |
parts of the main window. This avoids unnecessary duplication of work.
|
|
237 |
|
|
238 |
We create a toolbar as a child of the main window, and add the desired
|
|
239 |
actions to it:
|
|
240 |
|
|
241 |
\snippet examples/mainwindows/sdi/mainwindow.cpp 0
|
|
242 |
\dots
|
|
243 |
\snippet doc/src/snippets/code/doc_src_qt4-mainwindow.qdoc 1
|
|
244 |
|
|
245 |
In this example, the toolbar is restricted to the top and bottom
|
|
246 |
toolbar areas of the main window, and is initially placed in the
|
|
247 |
top tool bar area. We can see that the actions specified by \c
|
|
248 |
newAct and \c openAct will be displayed both on the toolbar and in
|
|
249 |
the file menu.
|
|
250 |
|
|
251 |
QDockWidget is used in a similar way to QToolBar. We create a
|
|
252 |
dock widget as a child of the main window, and add widgets as children
|
|
253 |
of the dock widget:
|
|
254 |
|
|
255 |
\snippet doc/src/snippets/dockwidgets/mainwindow.cpp 0
|
|
256 |
|
|
257 |
In this example, the dock widget can only be placed in the left and
|
|
258 |
right dock areas, and it is initially placed in the left dock area.
|
|
259 |
|
|
260 |
The QMainWindow API allows the programmer to customize which dock
|
|
261 |
widget areas occupy the four corners of the dock widget area. If
|
|
262 |
required, the default can be changed with the
|
|
263 |
QMainWindow::setCorner() function:
|
|
264 |
|
|
265 |
\snippet doc/src/snippets/code/doc_src_qt4-mainwindow.qdoc 2
|
|
266 |
|
|
267 |
The following diagram shows the configuration produced by the above code.
|
|
268 |
Note that the left and right dock widgets will occupy the top and bottom
|
|
269 |
corners of the main window in this layout.
|
|
270 |
|
|
271 |
\image mainwindow-docks-example.png
|
|
272 |
|
|
273 |
Once all of the main window components have been set up, the central widget
|
|
274 |
is created and installed by using code similar to the following:
|
|
275 |
|
|
276 |
\snippet doc/src/snippets/code/doc_src_qt4-mainwindow.qdoc 3
|
|
277 |
|
|
278 |
The central widget can be any subclass of QWidget.
|
|
279 |
*/
|