[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/toplevel/toplevel.doc:1 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Toplevel Widgets</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Toplevel Widgets</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | <p>
|
---|
| 37 | This example demonstrates the use of Qt's widget flags to provide
|
---|
| 38 | toplevel widgets with customized window decorations.
|
---|
| 39 | <p> It provides a graphical user interface for selecting different
|
---|
| 40 | options for widget decoration and behavior, and passes the
|
---|
| 41 | appropriate widget flags to the <a href="qwidget.html">QWidget</a> constructor.
|
---|
| 42 | <a href="qwidget.html#reparent">QWidget::reparent</a>() is used to change the widget flags at runtime.
|
---|
| 43 | <p> <b>Warning:</b> Note that the interpretation and functionality of the
|
---|
| 44 | widget flags depends on the window manager used when running the
|
---|
| 45 | application. Many window managers do not support every possible flag
|
---|
| 46 | combination.
|
---|
| 47 | <p> The user interface providing the different options was created using
|
---|
| 48 | <a href="designer-manual.html">Qt Designer</a>. The different
|
---|
| 49 | options are explained in the user interface through the use of
|
---|
| 50 | tooltips and What's This help. Load the <tt>options.ui</tt> file into
|
---|
| 51 | <a href="designer-manual.html">Qt Designer</a> for more details.
|
---|
| 52 | <p>
|
---|
| 53 |
|
---|
| 54 | <pre> #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 55 | #include "options.h"
|
---|
| 56 |
|
---|
| 57 | int main( int argc, char ** argv )
|
---|
| 58 | {
|
---|
| 59 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
| 60 | OptionsDialog dlg;
|
---|
| 61 | return dlg.exec();
|
---|
| 62 | }
|
---|
| 63 | </pre>
|
---|
| 64 | <p> The main function creates and displays the dialog for the user
|
---|
| 65 | interface. Note that this dialog is modal.
|
---|
| 66 | <p> The code relevant for this example is in the <tt>options.ui.h</tt>
|
---|
| 67 | file.
|
---|
| 68 | <p>
|
---|
| 69 |
|
---|
| 70 | <pre> void OptionsDialog::apply()
|
---|
| 71 | {
|
---|
| 72 | <a href="qstringlist.html">QStringList</a> flagList;
|
---|
| 73 | bool wstyle = false;
|
---|
| 74 | WFlags f = WDestructiveClose | WType_TopLevel | WStyle_Customize;
|
---|
| 75 | </pre>
|
---|
| 76 | <p> The <tt>apply()</tt> slot declares the <a href="qt.html#WidgetFlags">widget flag</a> variable <tt>f</tt>
|
---|
| 77 | and initializes it with the values
|
---|
| 78 | <ul>
|
---|
| 79 | <li> <tt>WDestructiveClose</tt> - the widget will be automatically
|
---|
| 80 | destroyed when it is closed,
|
---|
| 81 | <li> <tt>WType_TopLevel</tt> - the widget will be top level even if it
|
---|
| 82 | has a parent widget, and
|
---|
| 83 | <li> <tt>WStyle_Customize</tt> - the flags override the default values
|
---|
| 84 | </ul>
|
---|
| 85 | Other flags are used depending on the options selected in the user
|
---|
| 86 | interface.
|
---|
| 87 | <p> <pre> if ( bgBorder->isChecked() ) {
|
---|
| 88 | if ( rbBorderNormal->isChecked() ) {
|
---|
| 89 | f |= WStyle_NormalBorder;
|
---|
| 90 | flagList += "WStyle_NormalBorder";
|
---|
| 91 | wstyle = true;
|
---|
| 92 | }
|
---|
| 93 | else if ( rbBorderDialog->isChecked() ) {
|
---|
| 94 | f |= WStyle_DialogBorder;
|
---|
| 95 | flagList += "WStyle_DialogBorder";
|
---|
| 96 | wstyle = true;
|
---|
| 97 | }
|
---|
| 98 | </pre>The window gets a normal or dialog border depending on the selected
|
---|
| 99 | option.
|
---|
| 100 | <p> <pre> if ( bgTitle->isChecked() ) {
|
---|
| 101 | f |= WStyle_Title;
|
---|
| 102 | flagList += "WStyle_Title";
|
---|
| 103 | wstyle = true;
|
---|
| 104 | if ( cbTitleSystem->isChecked() ) {
|
---|
| 105 | f |= WStyle_SysMenu;
|
---|
| 106 | flagList += "WStyle_SysMenu";
|
---|
| 107 | }
|
---|
| 108 | if ( cbTitleMinimize->isChecked() ) {
|
---|
| 109 | f |= WStyle_Minimize;
|
---|
| 110 | flagList += "WStyle_Minimize";
|
---|
| 111 | }
|
---|
| 112 | if ( cbTitleMaximize->isChecked() ) {
|
---|
| 113 | f |= WStyle_Maximize;
|
---|
| 114 | flagList += "WStyle_Maximize";
|
---|
| 115 | }
|
---|
| 116 | if ( cbTitleContext->isChecked() ) {
|
---|
| 117 | f |= WStyle_ContextHelp;
|
---|
| 118 | flagList += "WStyle_ContextHelp";
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | </pre>A titlebar with controls is provided if the appropriate options
|
---|
| 122 | have been checked.
|
---|
| 123 | <p> <pre> } else {
|
---|
| 124 | f |= WStyle_NoBorder;
|
---|
| 125 | flagList += "WStyle_NoBorder";
|
---|
| 126 | wstyle = true;
|
---|
| 127 | }
|
---|
| 128 | </pre>If the window should not have a border it cannot have a titlebar.
|
---|
| 129 | Widgets that provide their own (e.g. themed) window decoration
|
---|
| 130 | should use this flag.
|
---|
| 131 | <p> <pre> <a href="qwidget.html">QWidget</a> *parent = this;
|
---|
| 132 | if ( cbBehaviorTaskbar->isChecked() ) {
|
---|
| 133 | parent = 0;
|
---|
| 134 | f |= WGroupLeader;
|
---|
| 135 | flagList += "WGroupLeader";
|
---|
| 136 | }
|
---|
| 137 | </pre>If a toplevel widget has a parent it will not have a taskbar
|
---|
| 138 | entry, and on most window managers it will always stay on
|
---|
| 139 | top of the parent widget. This is the standard behavior for
|
---|
| 140 | dialog boxes, especially if they are modeless, and for other
|
---|
| 141 | secondary toplevel widgets.
|
---|
| 142 | <p> To provide a taskbar entry the widget must have no parent,
|
---|
| 143 | in which case we need to use the <tt>WGroupLeader</tt> flag to
|
---|
| 144 | prevent blocking through the modal main dialog. Applications
|
---|
| 145 | that can have multiple toplevel windows open simultaneously
|
---|
| 146 | should use this combination.
|
---|
| 147 | <p> <pre> if ( cbBehaviorStays->isChecked() ) {
|
---|
| 148 | f |= WStyle_StaysOnTop /*| WX11BypassWM*/;
|
---|
| 149 | flagList += "WStyle_StaysOnTop";
|
---|
| 150 | wstyle = true;
|
---|
| 151 | }
|
---|
| 152 | </pre>A toplevel widget can stay on top of the whole desktop if the
|
---|
| 153 | window manager supports this functionality.
|
---|
| 154 | <a href="#footnote1"><sup>(1)</sup></a><a name="footnote-call1"></a>
|
---|
| 155 | <p> Widgets that display important or realtime information (i.e. IRC
|
---|
| 156 | clients) might benefit from using that flag.
|
---|
| 157 | <p> <pre> if ( cbBehaviorPopup->isChecked() ) {
|
---|
| 158 | f |= WType_Popup;
|
---|
| 159 | flagList += "WType_Popup";
|
---|
| 160 | }
|
---|
| 161 | </pre>A popup widget is a short lived modal widget that closes
|
---|
| 162 | automatically. Popup menus are a typical example for such widgets.
|
---|
| 163 | <p> <pre> if ( cbBehaviorModal->isChecked() ) {
|
---|
| 164 | f |= WShowModal;
|
---|
| 165 | flagList += "WShowModal";
|
---|
| 166 | }
|
---|
| 167 | </pre>A modal widget blocks input to other toplevel widgets, unless
|
---|
| 168 | those are in a different modal group (see <tt>WGroupLeader</tt>).
|
---|
| 169 | Dialogs are often modal, and the <a href="qdialog.html">QDialog</a> class provides an easy API
|
---|
| 170 | to create and display them without the need to explicitly use this
|
---|
| 171 | flag.
|
---|
| 172 | <p> <pre> if ( cbBehaviorTool->isChecked() ) {
|
---|
| 173 | f |= WStyle_Tool;
|
---|
| 174 | flagList += "WStyle_Tool";
|
---|
| 175 | wstyle = true;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | if (wstyle)
|
---|
| 179 | <a name="x2534"></a> flagList.<a href="qvaluelist.html#push_front">push_front</a>("WStyle_Customize");
|
---|
| 180 | </pre>A tool window will never have a task bar entry (even if it
|
---|
| 181 | has no parent widget), and often has a smaller window
|
---|
| 182 | decoration. Tool windows are frequently used instead of
|
---|
| 183 | modeless dialogs.
|
---|
| 184 | <p> <pre> if ( !widget ) {
|
---|
| 185 | widget = new <a href="qvbox.html">QVBox</a>( parent, 0, f );
|
---|
| 186 | <a name="x2530"></a> widget-><a href="qframe.html#setMargin">setMargin</a>( 20 );
|
---|
| 187 | <a name="x2533"></a> <a href="qlabel.html">QLabel</a> *label = new <a href="qlabel.html">QLabel</a>(flagList.<a href="qstringlist.html#join">join</a>("&nbsp;| "), widget);
|
---|
| 188 | <a name="x2532"></a> label-><a href="qlabel.html#setTextFormat">setTextFormat</a>(RichText);
|
---|
| 189 | <a name="x2531"></a> label-><a href="qlabel.html#setAlignment">setAlignment</a>(WordBreak);
|
---|
| 190 | <a href="qpushbutton.html">QPushButton</a> *okButton = new <a href="qpushbutton.html">QPushButton</a>( "Close", widget );
|
---|
| 191 | <a name="x2535"></a> connect( okButton, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), widget, SLOT(<a href="qwidget.html#close">close</a>()) );
|
---|
| 192 | <a name="x2537"></a> widget-><a href="qwidget.html#move">move</a>( pos() );
|
---|
| 193 | </pre>The widget is created if it has not been created yet, or if it was
|
---|
| 194 | closed (since we use the <tt>WDestructiveClose</tt> flag). Note that the
|
---|
| 195 | window is not visible yet.
|
---|
| 196 | <a href="#footnote2"><sup>(2)</sup></a><a name="footnote-call2"></a>
|
---|
| 197 | <p> <pre> } else {
|
---|
| 198 | <a name="x2538"></a><a name="x2536"></a> widget-><a href="qwidget.html#reparent">reparent</a>( parent, f, widget-><a href="qwidget.html#geometry">geometry</a>().topLeft(), FALSE);
|
---|
| 199 | }
|
---|
| 200 | </pre>If the widget has already been created the reparent() function is
|
---|
| 201 | used to modify the widget's flags. The widget's geometry is not
|
---|
| 202 | changed, and the window is not shown again.
|
---|
| 203 | <p> <pre> <a name="x2539"></a> widget-><a href="qwidget.html#setCaption">setCaption</a>( leCaption->text() );
|
---|
| 204 | <a name="x2540"></a> widget-><a href="qwidget.html#setIcon">setIcon</a>( leIcon->text() );
|
---|
| 205 | <a name="x2541"></a> widget-><a href="qwidget.html#setWindowOpacity">setWindowOpacity</a>(double(slTransparency->maxValue() - slTransparency->value()) / 100);
|
---|
| 206 |
|
---|
| 207 | <a name="x2542"></a> widget-><a href="qwidget.html#show">show</a>();
|
---|
| 208 | </pre>Finally the higher level properties such as the window's caption and
|
---|
| 209 | icon are set. The window transparency is set according to the slider
|
---|
| 210 | value. Note that this will only have effect on systems that support
|
---|
| 211 | this attribute for toplevel window.
|
---|
| 212 | <p> <pre> }
|
---|
| 213 | </pre>Finally the window is shown with the new attributes.
|
---|
| 214 | <p> To build the example go to the toplevel directory
|
---|
| 215 | (<tt>QTDIR/examples/toplevel</tt>)
|
---|
| 216 | <a href="#footnote3"><sup>(3)</sup></a><a name="footnote-call3"></a>
|
---|
| 217 | and run <tt>qmake</tt> to generate the makefile, then use the make tool to
|
---|
| 218 | build the library.
|
---|
| 219 |
|
---|
| 220 | <hr>
|
---|
| 221 | <ol> <li><a name="footnote1"></a>
|
---|
| 222 | Unfortunately some X11 window managers also require the <tt>WX11BypassWM</tt> flag to be set in addition; but some other X11 window
|
---|
| 223 | managers will have problems if this flag is set. <a href="#footnote-call1">Back...</a> <li><a name="footnote2"></a>
|
---|
| 224 | The example uses <a href="qguardedptr.html">QGuardedPtr</a> to make sure that the
|
---|
| 225 | pointer is reset to zero when the widget object is destroyed
|
---|
| 226 | due to the <tt>WDestructiveClose</tt> flag. <a href="#footnote-call2">Back...</a> <li><a name="footnote3"></a>
|
---|
| 227 |
|
---|
| 228 | We use <tt>QTDIR</tt> to stand for the directory where Qt is installed.
|
---|
| 229 | <a href="#footnote-call3">Back...</a></ol>
|
---|
| 230 | </hr><p>See also <a href="examples.html">Examples</a>.
|
---|
| 231 |
|
---|
| 232 | <!-- eof -->
|
---|
| 233 | <p><address><hr><div align=center>
|
---|
| 234 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 235 | <td>Copyright © 2007
|
---|
| 236 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 237 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 238 | </table></div></address></body>
|
---|
| 239 | </html>
|
---|