1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/windowflags.qdoc -->
7
<title>Qt 4.0: Window Flags Example</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
19
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Window Flags Example</h1>
23
<li><a href="widgets-windowflags-controllerwindow-cpp.html">widgets/windowflags/controllerwindow.cpp</a></li>
24
<li><a href="widgets-windowflags-controllerwindow-h.html">widgets/windowflags/controllerwindow.h</a></li>
25
<li><a href="widgets-windowflags-previewwindow-cpp.html">widgets/windowflags/previewwindow.cpp</a></li>
26
<li><a href="widgets-windowflags-previewwindow-h.html">widgets/windowflags/previewwindow.h</a></li>
27
<li><a href="widgets-windowflags-main-cpp.html">widgets/windowflags/main.cpp</a></li>
29
<p>The Window Flags example shows how to use the window flags available in Qt.</p>
30
<p>A window flag is either a type or a hint. A type is used to specify various window-system properties for the widget. A widget can only have one type, and the default is <a href="qt.html#WindowType-enum">Qt::Widget</a>. However, a widget can have zero or more hints. The hints are used to customize the appearance of top-level windows.</p>
31
<p>A widget's flags are stored in a <a href="qt.html#WindowType-enum">Qt::WindowFlags</a> type which stores an OR combination of the flags.</p>
32
<center><img src="images/windowflags-example.png" alt="Screenshot of the Window Flags example" /></center><p>The example consists of two classes:</p>
34
<li><tt>ControllerWindow</tt> is the main application widget that allows the user to choose among the available window flags, and displays the effect on a separate preview window.</li>
35
<li><tt>PreviewWindow</tt> is a custom widget displaying the name of its currently set window flags in a read-only text editor.</li>
37
<p>We will start by reviewing the <tt>ControllerWindow</tt> class, then we will take a look at the <tt>PreviewWindow</tt> class.</p>
38
<a name="controllerwindow-class-definition"></a>
39
<h2>ControllerWindow Class Definition</h2>
40
<pre> class ControllerWindow : public QWidget
51
void createTypeGroupBox();
52
void createHintsGroupBox();
53
QCheckBox *createCheckBox(const QString &text);
54
QRadioButton *createRadioButton(const QString &text);
56
PreviewWindow *previewWindow;
58
QGroupBox *typeGroupBox;
59
QGroupBox *hintsGroupBox;
60
QPushButton *quitButton;
62
QRadioButton *windowRadioButton;
63
QRadioButton *dialogRadioButton;
64
QRadioButton *sheetRadioButton;
65
QRadioButton *drawerRadioButton;
66
QRadioButton *popupRadioButton;
67
QRadioButton *toolRadioButton;
68
QRadioButton *toolTipRadioButton;
69
QRadioButton *splashScreenRadioButton;
71
QCheckBox *msWindowsFixedSizeDialogCheckBox;
72
QCheckBox *x11BypassWindowManagerCheckBox;
73
QCheckBox *framelessWindowCheckBox;
74
QCheckBox *windowTitleCheckBox;
75
QCheckBox *windowSystemMenuCheckBox;
76
QCheckBox *windowMinimizeButtonCheckBox;
77
QCheckBox *windowMaximizeButtonCheckBox;
78
QCheckBox *windowContextHelpButtonCheckBox;
79
QCheckBox *windowShadeButtonCheckBox;
80
QCheckBox *windowStaysOnTopCheckBox;
82
<p>The <tt>ControllerWindow</tt> class inherits <a href="qwidget.html">QWidget</a>. The widget allows the user to choose among the available window flags, and displays the effect on a separate preview window.</p>
83
<p>We declare a private <tt>updatePreview()</tt> slot to refresh the preview window whenever the user changes the window flags.</p>
84
<p>We also declare several private functions to simplify the constructor: We call the <tt>createTypeGroupBox()</tt> function to create a radio button for each available window type, using the private <tt>createButton()</tt> function, and gather them within a group box. In a similar way we use the <tt>createHintsGroupBox()</tt> function to create a check box for each available hint, using the private <tt>createCheckBox()</tt> function.</p>
85
<p>In addition to the various radio buttons and checkboxes, we need an associated <tt>PreviewWindow</tt> to show the effect of the currently chosen window flags.</p>
86
<center><img src="images/windowflags_controllerwindow.png" alt="Screenshot of the Controller Window" /></center><a name="controllerwindow-class-implementation"></a>
87
<h2>ControllerWindow Class Implementation</h2>
88
<pre> ControllerWindow::ControllerWindow()
90
previewWindow = new PreviewWindow(this);
93
createHintsGroupBox();
95
quitButton = new QPushButton(tr("&Quit"));
96
connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
98
QHBoxLayout *bottomLayout = new QHBoxLayout;
99
bottomLayout->addStretch();
100
bottomLayout->addWidget(quitButton);
102
QVBoxLayout *mainLayout = new QVBoxLayout;
103
mainLayout->addWidget(typeGroupBox);
104
mainLayout->addWidget(hintsGroupBox);
105
mainLayout->addLayout(bottomLayout);
106
setLayout(mainLayout);
108
setWindowTitle(tr("Window Flags"));
111
<p>In the constructor we first create the preview window. Then we create the group boxes containing the available window flags using the private <tt>createTypeGroupBox()</tt> and <tt>createHintsGroupBox()</tt> functions. In addition we create a <b>Quit</b> button. We put the button and a stretchable space in a separate layout to make the button appear in the <tt>WindowFlag</tt> widget's right bottom corner.</p>
112
<p>Finally, we add the button's layout and the two goup boxes to a <a href="qvboxlayout.html">QVBoxLayout</a>, set the window title and refresh the preview window using the <tt>updatePreview()</tt> slot.</p>
113
<pre> void ControllerWindow::updatePreview()
115
Qt::WindowFlags flags = 0;
117
if (windowRadioButton->isChecked()) {
119
} else if (dialogRadioButton->isChecked()) {
121
} else if (sheetRadioButton->isChecked()) {
123
} else if (drawerRadioButton->isChecked()) {
125
} else if (popupRadioButton->isChecked()) {
127
} else if (toolRadioButton->isChecked()) {
129
} else if (toolTipRadioButton->isChecked()) {
131
} else if (splashScreenRadioButton->isChecked()) {
132
flags = Qt::SplashScreen;
134
<p>The <tt>updatePreview()</tt> slot is called whenever the user changes any of the window flags. First we create an empty <a href="qt.html#WindowType-enum">Qt::WindowFlags</a> <tt>flags</tt>, then we determine which one of the types that is checked and add it to <tt>flags</tt>.</p>
135
<pre> if (msWindowsFixedSizeDialogCheckBox->isChecked())
136
flags |= Qt::MSWindowsFixedSizeDialogHint;
137
if (x11BypassWindowManagerCheckBox->isChecked())
138
flags |= Qt::X11BypassWindowManagerHint;
139
if (framelessWindowCheckBox->isChecked())
140
flags |= Qt::FramelessWindowHint;
141
if (windowTitleCheckBox->isChecked())
142
flags |= Qt::WindowTitleHint;
143
if (windowSystemMenuCheckBox->isChecked())
144
flags |= Qt::WindowSystemMenuHint;
145
if (windowMinimizeButtonCheckBox->isChecked())
146
flags |= Qt::WindowMinimizeButtonHint;
147
if (windowMaximizeButtonCheckBox->isChecked())
148
flags |= Qt::WindowMaximizeButtonHint;
149
if (windowContextHelpButtonCheckBox->isChecked())
150
flags |= Qt::WindowContextHelpButtonHint;
151
if (windowShadeButtonCheckBox->isChecked())
152
flags |= Qt::WindowShadeButtonHint;
153
if (windowStaysOnTopCheckBox->isChecked())
154
flags |= Qt::WindowStaysOnTopHint;
156
previewWindow->setWindowFlags(flags);
157
previewWindow->show();</pre>
158
<p>We also determine which of the hints that are checked, and add them to <tt>flags</tt> using an OR operator. We use <tt>flags</tt> to set the window flags for the preview window, and call <a href="qwidget.html#show">QWidget::show</a>() to make sure the preview window is visible.</p>
159
<pre> QPoint pos = previewWindow->pos();
164
previewWindow->move(pos);
166
<p>Finally, we adjust the position of the preview window. The reason we do that, is that playing around with the window's frame may on some platforms cause the window's position to be changed behind our back. If a window is located in the upper left corner of the screen, parts of the window may not be visible. So we adjust the widget's position to make sure that, if this happens, the window is moved within the screen's boundaries.</p>
167
<pre> void ControllerWindow::createTypeGroupBox()
169
typeGroupBox = new QGroupBox(tr("Type"));
171
windowRadioButton = createRadioButton(tr("Window"));
172
dialogRadioButton = createRadioButton(tr("Dialog"));
173
sheetRadioButton = createRadioButton(tr("Sheet"));
174
drawerRadioButton = createRadioButton(tr("Drawer"));
175
popupRadioButton = createRadioButton(tr("Popup"));
176
toolRadioButton = createRadioButton(tr("Tool"));
177
toolTipRadioButton = createRadioButton(tr("Tooltip"));
178
splashScreenRadioButton = createRadioButton(tr("Splash screen"));
179
windowRadioButton->setChecked(true);
181
QGridLayout *layout = new QGridLayout;
182
layout->addWidget(windowRadioButton, 0, 0);
183
layout->addWidget(dialogRadioButton, 1, 0);
184
layout->addWidget(sheetRadioButton, 2, 0);
185
layout->addWidget(drawerRadioButton, 3, 0);
186
layout->addWidget(popupRadioButton, 0, 1);
187
layout->addWidget(toolRadioButton, 1, 1);
188
layout->addWidget(toolTipRadioButton, 2, 1);
189
layout->addWidget(splashScreenRadioButton, 3, 1);
190
typeGroupBox->setLayout(layout);
192
<p>The private <tt>createTypeGroupBox()</tt> function is called from the constructor.</p>
193
<p>First we create a group box, and then we create a radio button (using the private <tt>createRadioButton()</tt> function) for each of the available types among the window flags. We make <a href="qt.html#WindowType-enum">Qt::Window</a> the initially applied type. We put the radio buttons into a <a href="qgridlayout.html">QGridLayout</a> and install the layout on the group box.</p>
194
<p>We do not include the default <a href="qt.html#WindowType-enum">Qt::Widget</a> type. The reason is that it behaves somewhat different than the other types. If the type is not specified for a widget, and it has no parent, the widget is a window. However, if it has a parent, it is a standard child widget. The other types are all top-level windows, and since the hints only affect top-level windows, we abandon the <a href="qt.html#WindowType-enum">Qt::Widget</a> type.</p>
195
<pre> void ControllerWindow::createHintsGroupBox()
197
hintsGroupBox = new QGroupBox(tr("Hints"));
199
msWindowsFixedSizeDialogCheckBox =
200
createCheckBox(tr("MS Windows fixed size dialog"));
201
x11BypassWindowManagerCheckBox =
202
createCheckBox(tr("X11 bypass window manager"));
203
framelessWindowCheckBox = createCheckBox(tr("Frameless window"));
204
windowTitleCheckBox = createCheckBox(tr("Window title"));
205
windowSystemMenuCheckBox = createCheckBox(tr("Window system menu"));
206
windowMinimizeButtonCheckBox = createCheckBox(tr("Window minimize button"));
207
windowMaximizeButtonCheckBox = createCheckBox(tr("Window maximize button"));
208
windowContextHelpButtonCheckBox =
209
createCheckBox(tr("Window context help button"));
210
windowShadeButtonCheckBox = createCheckBox(tr("Window shade button"));
211
windowStaysOnTopCheckBox = createCheckBox(tr("Window stays on top"));
213
QGridLayout *layout = new QGridLayout;
214
layout->addWidget(msWindowsFixedSizeDialogCheckBox, 0, 0);
215
layout->addWidget(x11BypassWindowManagerCheckBox, 1, 0);
216
layout->addWidget(framelessWindowCheckBox, 2, 0);
217
layout->addWidget(windowTitleCheckBox, 3, 0);
218
layout->addWidget(windowSystemMenuCheckBox, 4, 0);
219
layout->addWidget(windowMinimizeButtonCheckBox, 0, 1);
220
layout->addWidget(windowMaximizeButtonCheckBox, 1, 1);
221
layout->addWidget(windowContextHelpButtonCheckBox, 2, 1);
222
layout->addWidget(windowShadeButtonCheckBox, 3, 1);
223
layout->addWidget(windowStaysOnTopCheckBox, 4, 1);
224
hintsGroupBox->setLayout(layout);
226
<p>The private <tt>createHintsGroupBox()</tt> function is also called from the constructor.</p>
227
<p>Again, the first thing we do is to create a group box. Then we create a checkbox, using the private <tt>createCheckBox()</tt> function, for each of the available hints among the window flags. We put the checkboxes into a <a href="qgridlayout.html">QGridLayout</a> and install the layout on the group box.</p>
228
<pre> QCheckBox *ControllerWindow::createCheckBox(const QString &text)
230
QCheckBox *checkBox = new QCheckBox(text);
231
connect(checkBox, SIGNAL(clicked()), this, SLOT(updatePreview()));
234
<p>The private <tt>createCheckBox()</tt> function is called from <tt>createHintsGroupBox()</tt>.</p>
235
<p>We simply create a <a href="qcheckbox.html">QCheckBox</a> with the provided text, connect it to the private <tt>updatePreview()</tt> slot, and return a pointer to the checkbox.</p>
236
<pre> QRadioButton *ControllerWindow::createRadioButton(const QString &text)
238
QRadioButton *button = new QRadioButton(text);
239
connect(button, SIGNAL(clicked()), this, SLOT(updatePreview()));
242
<p>In the private <tt>createRadioButton()</tt> function it is a <a href="qradiobutton.html">QRadioButton</a> we create with the provided text, and connect to the private <tt>updatePreview()</tt> slot. The function is called from <tt>createTypeGroupBox()</tt>, and returns a pointer to the button.</p>
243
<a name="previewwindow-class-definition"></a>
244
<h2>PreviewWindow Class Definition</h2>
245
<pre> class PreviewWindow : public QWidget
250
PreviewWindow(QWidget *parent = 0);
252
void setWindowFlags(Qt::WindowFlags flags);
256
QPushButton *closeButton;
258
<p>The <tt>PreviewWindow</tt> class inherits <a href="qwidget.html">QWidget</a>. It is a custom widget that displays the names of its currently set window flags in a read-only text editor. It is also provided with a <a href="porting4.html#qpushbutton">QPushbutton</a> that closes the window.</p>
259
<p>We reimplement the constructor to create the <b>Close</b> button and the text editor, and the <a href="qwidget.html#windowFlags-prop">QWidget::setWindowFlags</a>() function to display the names of the window flags.</p>
260
<center><img src="images/windowflags_previewwindow.png" alt="Screenshot of the Preview Window" /></center><a name="previewwindow-class-implementation"></a>
261
<h2>PreviewWindow Class Implementation</h2>
262
<pre> PreviewWindow::PreviewWindow(QWidget *parent)
265
textEdit = new QTextEdit;
266
textEdit->setReadOnly(true);
267
textEdit->setLineWrapMode(QTextEdit::NoWrap);
269
closeButton = new QPushButton(tr("&Close"));
270
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
272
QVBoxLayout *layout = new QVBoxLayout;
273
layout->addWidget(textEdit);
274
layout->addWidget(closeButton);
277
setWindowTitle(tr("Preview"));
279
<p>In the constructor, we first create a <a href="qtextedit.html">QTextEdit</a> and make sure that it is read-only.</p>
280
<p>We also prohibit any line wrapping in the text editor using the <a href="qtextedit.html#lineWrapMode-prop">QTextEdit::setLineWrapMode</a>() function. The result is that a horizontal scrollbar appears when a window flag's name exceeds the width of the editor. This is a reasonable solution since we construct the displayed text with built-in line breaks. If no line breaks were guaranteed, using another <a href="qtextedit.html#LineWrapMode-enum">QTextEdit::LineWrapMode</a> would perhaps make more sense.</p>
281
<p>Then we create the <b>Close</b> button, and put both the widgets into a <a href="qvboxlayout.html">QVBoxLayout</a> before we set the window title.</p>
282
<pre> void PreviewWindow::setWindowFlags(Qt::WindowFlags flags)
284
QWidget::setWindowFlags(flags);
288
Qt::WindowFlags type = (flags & Qt::WindowType_Mask);
289
if (type == Qt::Window) {
290
text = "Qt::Window";
291
} else if (type == Qt::Dialog) {
292
text = "Qt::Dialog";
293
} else if (type == Qt::Sheet) {
294
text = "Qt::Sheet";
295
} else if (type == Qt::Drawer) {
296
text = "Qt::Drawer";
297
} else if (type == Qt::Popup) {
298
text = "Qt::Popup";
299
} else if (type == Qt::Tool) {
300
text = "Qt::Tool";
301
} else if (type == Qt::ToolTip) {
302
text = "Qt::ToolTip";
303
} else if (type == Qt::SplashScreen) {
304
text = "Qt::SplashScreen";
307
if (flags & Qt::MSWindowsFixedSizeDialogHint)
308
text += "\n| Qt::MSWindowsFixedSizeDialogHint";
309
if (flags & Qt::X11BypassWindowManagerHint)
310
text += "\n| Qt::X11BypassWindowManagerHint";
311
if (flags & Qt::FramelessWindowHint)
312
text += "\n| Qt::FramelessWindowHint";
313
if (flags & Qt::WindowTitleHint)
314
text += "\n| Qt::WindowTitleHint";
315
if (flags & Qt::WindowSystemMenuHint)
316
text += "\n| Qt::WindowSystemMenuHint";
317
if (flags & Qt::WindowMinimizeButtonHint)
318
text += "\n| Qt::WindowMinimizeButtonHint";
319
if (flags & Qt::WindowMaximizeButtonHint)
320
text += "\n| Qt::WindowMaximizeButtonHint";
321
if (flags & Qt::WindowContextHelpButtonHint)
322
text += "\n| Qt::WindowContextHelpButtonHint";
323
if (flags & Qt::WindowShadeButtonHint)
324
text += "\n| Qt::WindowShadeButtonHint";
325
if (flags & Qt::WindowStaysOnTopHint)
326
text += "\n| Qt::WindowStaysOnTopHint";
328
textEdit->setPlainText(text);
330
<p>In our reimplementation of the <tt>setWindowFlags()</tt> function, we first set the widgets flags using the <a href="qwidget.html#windowFlags-prop">QWidget::setWindowFlags</a>() function. Then we run through the available window flags, creating a text that contains the names of the flags that matches the <tt>flags</tt> parameter. Finally, we display the text in the widgets text editor.</p>
331
<p /><address><hr /><div align="center">
332
<table width="100%" cellspacing="0" border="0"><tr class="address">
333
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
334
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
335
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
336
</tr></table></div></address></body>