| 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/doc/application-walkthrough.doc:36 --> | 
|---|
| 3 | <html> | 
|---|
| 4 | <head> | 
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|---|
| 6 | <title>Walkthrough: A Simple Application</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>Walkthrough: A Simple Application</h1> | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | <p> | 
|---|
| 37 | <p> This walkthrough shows simple use of <a href="qmainwindow.html">QMainWindow</a>, <a href="qmenubar.html">QMenuBar</a>, <a href="qpopupmenu.html">QPopupMenu</a>, <a href="qtoolbar.html">QToolBar</a> and <a href="qstatusbar.html">QStatusBar</a> - classes that every | 
|---|
| 38 | modern application window tends to use. (See also <a href="tutorial2.html">Tutorial #2</a>.) | 
|---|
| 39 | <p> It also illustrates some aspects of <a href="qwhatsthis.html">QWhatsThis</a> (for simple help) and a | 
|---|
| 40 | typical <tt>main()</tt> using <a href="qapplication.html">QApplication</a>. | 
|---|
| 41 | <p> Finally, it shows a typical print function based on <a href="qprinter.html">QPrinter</a>. | 
|---|
| 42 | <p> <h2> The declaration of ApplicationWindow | 
|---|
| 43 | </h2> | 
|---|
| 44 | <a name="1"></a><p> Here's the header file in full: | 
|---|
| 45 | <p> <pre>/**************************************************************************** | 
|---|
| 46 | ** $Id: simple-application.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 47 | ** | 
|---|
| 48 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 49 | ** | 
|---|
| 50 | ** This file is part of an example program for Qt.  This example | 
|---|
| 51 | ** program may be used, distributed and modified without limitation. | 
|---|
| 52 | ** | 
|---|
| 53 | *****************************************************************************/ | 
|---|
| 54 |  | 
|---|
| 55 | #ifndef APPLICATION_H | 
|---|
| 56 | #define APPLICATION_H | 
|---|
| 57 |  | 
|---|
| 58 | #include <<a href="qmainwindow-h.html">qmainwindow.h</a>> | 
|---|
| 59 |  | 
|---|
| 60 | class QTextEdit; | 
|---|
| 61 |  | 
|---|
| 62 | class ApplicationWindow: public <a href="qmainwindow.html">QMainWindow</a> | 
|---|
| 63 | { | 
|---|
| 64 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> | 
|---|
| 65 |  | 
|---|
| 66 | public: | 
|---|
| 67 | ApplicationWindow(); | 
|---|
| 68 | ~ApplicationWindow(); | 
|---|
| 69 |  | 
|---|
| 70 | protected: | 
|---|
| 71 | void closeEvent( <a href="qcloseevent.html">QCloseEvent</a>* ); | 
|---|
| 72 |  | 
|---|
| 73 | private slots: | 
|---|
| 74 | void newDoc(); | 
|---|
| 75 | void choose(); | 
|---|
| 76 | void load( const <a href="qstring.html">QString</a> &fileName ); | 
|---|
| 77 | void save(); | 
|---|
| 78 | void saveAs(); | 
|---|
| 79 | void print(); | 
|---|
| 80 |  | 
|---|
| 81 | void about(); | 
|---|
| 82 | void aboutQt(); | 
|---|
| 83 |  | 
|---|
| 84 | private: | 
|---|
| 85 | <a href="qprinter.html">QPrinter</a> *printer; | 
|---|
| 86 | <a href="qtextedit.html">QTextEdit</a> *e; | 
|---|
| 87 | <a href="qstring.html">QString</a> filename; | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | #endif | 
|---|
| 92 | </pre> | 
|---|
| 93 |  | 
|---|
| 94 | <p> It declares a class that inherits <a href="qmainwindow.html">QMainWindow</a>, with slots and private | 
|---|
| 95 | variables. The class pre-declaration of <a href="qtextedit.html">QTextEdit</a> at the beginning | 
|---|
| 96 | (instead of an include) helps to speed up compilation. With this | 
|---|
| 97 | trick, <tt>make depend</tt> won't insist on recompiling every <tt>.cpp</tt> file that | 
|---|
| 98 | includes <tt>application.h</tt> when <a href="qtextedit-h.html">qtextedit.h</a> changes. | 
|---|
| 99 | <p> <a name="simplemain"></a> | 
|---|
| 100 | <h2> A simple main() | 
|---|
| 101 | </h2> | 
|---|
| 102 | <a name="2"></a><p> Here is <tt>main.cpp</tt> in full: | 
|---|
| 103 | <p> <pre>/**************************************************************************** | 
|---|
| 104 | ** $Id: simple-application.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 105 | ** | 
|---|
| 106 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 107 | ** | 
|---|
| 108 | ** This file is part of an example program for Qt.  This example | 
|---|
| 109 | ** program may be used, distributed and modified without limitation. | 
|---|
| 110 | ** | 
|---|
| 111 | *****************************************************************************/ | 
|---|
| 112 |  | 
|---|
| 113 | #include <<a href="qapplication-h.html">qapplication.h</a>> | 
|---|
| 114 | #include "application.h" | 
|---|
| 115 |  | 
|---|
| 116 | int main( int argc, char ** argv ) { | 
|---|
| 117 | <a href="qapplication.html">QApplication</a> a( argc, argv ); | 
|---|
| 118 | ApplicationWindow *mw = new ApplicationWindow(); | 
|---|
| 119 | mw-><a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Application" ); | 
|---|
| 120 | <a name="x1598"></a>    mw-><a href="qwidget.html#show">show</a>(); | 
|---|
| 121 | <a name="x1599"></a>    a.<a href="qobject.html#connect">connect</a>( &a, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), &a, SLOT(<a href="qapplication.html#quit">quit</a>()) ); | 
|---|
| 122 | return a.<a href="qapplication.html#exec">exec</a>(); | 
|---|
| 123 | } | 
|---|
| 124 | </pre> | 
|---|
| 125 |  | 
|---|
| 126 | <p> Now we'll look at <tt>main.cpp</tt> in detail. | 
|---|
| 127 | <p> | 
|---|
| 128 |  | 
|---|
| 129 | <pre>    int main( int argc, char ** argv ) { | 
|---|
| 130 | <a href="qapplication.html">QApplication</a> a( argc, argv ); | 
|---|
| 131 | </pre> | 
|---|
| 132 | <p> With the above line, we create a <a href="qapplication.html">QApplication</a> object with the usual | 
|---|
| 133 | constructor and let it | 
|---|
| 134 | parse <em>argc</em> and <em>argv</em>. QApplication itself takes care of X11-specific | 
|---|
| 135 | command-line options like <em>-geometry</em>, so the program will automatically | 
|---|
| 136 | behave the way X clients are expected to. | 
|---|
| 137 | <p> <pre>        ApplicationWindow *mw = new ApplicationWindow(); | 
|---|
| 138 | mw-><a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Application" ); | 
|---|
| 139 | <a name="x2115"></a>    mw-><a href="qwidget.html#show">show</a>(); | 
|---|
| 140 | </pre> | 
|---|
| 141 | <p> We create an <em>ApplicationWindow</em> as a top-level widget, set its window | 
|---|
| 142 | system caption to "Document 1", and <em>show()</em> it. | 
|---|
| 143 | <p> <a name="close"></a> | 
|---|
| 144 | <pre>        a.<a href="qobject.html#connect">connect</a>( &a, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), &a, SLOT(<a href="qapplication.html#quit">quit</a>()) ); | 
|---|
| 145 | </pre> | 
|---|
| 146 | <p> When the application's last window is closed, it should quit. Both | 
|---|
| 147 | the signal and the slot are predefined members of <a href="qapplication.html">QApplication</a>. | 
|---|
| 148 | <p> <pre>        return a.<a href="qapplication.html#exec">exec</a>(); | 
|---|
| 149 | </pre> | 
|---|
| 150 | <p> Having completed the application's initialization, we start the main | 
|---|
| 151 | event loop (the GUI), and eventually return the error code | 
|---|
| 152 | that QApplication returns when it leaves the event loop. | 
|---|
| 153 | <p> <pre>    } | 
|---|
| 154 | </pre> | 
|---|
| 155 | <p> <a name="ApplicationWindow"></a> | 
|---|
| 156 | <h2> The Implementation of ApplicationWindow | 
|---|
| 157 | </h2> | 
|---|
| 158 | <a name="3"></a><p> | 
|---|
| 159 |  | 
|---|
| 160 | <p> Since the implementation is quite large (almost 300 lines) we | 
|---|
| 161 | won't list the whole thing. (The source code is included in the | 
|---|
| 162 | examples/application directory.) Before we start with the constructor | 
|---|
| 163 | there are three <tt>#include</tt>s worth mentioning: | 
|---|
| 164 | <p> <pre>    #include "filesave.xpm" | 
|---|
| 165 | #include "fileopen.xpm" | 
|---|
| 166 | #include "fileprint.xpm" | 
|---|
| 167 | </pre> | 
|---|
| 168 | <p> The tool buttons in our application wouldn't look good without icons! | 
|---|
| 169 | These icons can be found in the XPM files included above. If you ever | 
|---|
| 170 | moved a program to a different location and wondered why icons were | 
|---|
| 171 | missing afterwards you will probably agree that it is a good idea to | 
|---|
| 172 | compile them into the binary. This is what we are doing here. | 
|---|
| 173 | <p> <pre>    ApplicationWindow::ApplicationWindow() | 
|---|
| 174 | : <a href="qmainwindow.html">QMainWindow</a>( 0, "example application main window", WDestructiveClose | WGroupLeader ) | 
|---|
| 175 | { | 
|---|
| 176 | </pre> | 
|---|
| 177 | <p> <em>ApplicationWindow</em> inherits <a href="qmainwindow.html">QMainWindow</a>, the Qt class that provides | 
|---|
| 178 | typical application main windows, with menu bars, toolbars, etc. | 
|---|
| 179 | <p> <pre>        printer = new <a href="qprinter.html">QPrinter</a>( QPrinter::HighResolution ); | 
|---|
| 180 | </pre> | 
|---|
| 181 | <p> The application example can print things, and we chose to have a | 
|---|
| 182 | <a href="qprinter.html">QPrinter</a> object lying around so that when the user changes a setting | 
|---|
| 183 | during one printing, the new setting will be the default next time. | 
|---|
| 184 | <p> <pre>        <a href="qpixmap.html">QPixmap</a> openIcon, saveIcon, printIcon; | 
|---|
| 185 | </pre> | 
|---|
| 186 | <p> For the sake of simplicity, our example only has a few commands in the | 
|---|
| 187 | toolbar. The above variables are used to hold an icon for each of | 
|---|
| 188 | them. | 
|---|
| 189 | <p> <pre>        <a href="qtoolbar.html">QToolBar</a> * fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" ); | 
|---|
| 190 | </pre> | 
|---|
| 191 | <p> We create a toolbar in <em>this</em> window ... | 
|---|
| 192 | <p> <pre>        fileTools-><a href="qtoolbar.html#setLabel">setLabel</a>( "File Operations" ); | 
|---|
| 193 | </pre> | 
|---|
| 194 | <p> ... and define a title for it. When a user drags the toolbar out of | 
|---|
| 195 | its location and floats it over the desktop, the toolbar-window will | 
|---|
| 196 | show "File Operations" as caption. | 
|---|
| 197 | <p> <pre>        openIcon = QPixmap( fileopen ); | 
|---|
| 198 | QToolButton * fileOpen | 
|---|
| 199 | = new <a href="qtoolbutton.html">QToolButton</a>( openIcon, "Open File", <a href="qstring.html#QString-null">QString::null</a>, | 
|---|
| 200 | this, SLOT(choose()), fileTools, "open file" ); | 
|---|
| 201 | </pre> | 
|---|
| 202 | <p> Now we create the first tool button for the <em>fileTools</em> toolbar | 
|---|
| 203 | with the appropriate icon and the tool-tip text "Open File". | 
|---|
| 204 | The <tt>fileopen.xpm</tt> we included at the beginning | 
|---|
| 205 | contains the definition of a pixmap called <em>fileopen</em>. | 
|---|
| 206 | We use this icon to illustrate our first tool button. | 
|---|
| 207 | <p> <pre>        saveIcon = QPixmap( filesave ); | 
|---|
| 208 | QToolButton * fileSave | 
|---|
| 209 | = new <a href="qtoolbutton.html">QToolButton</a>( saveIcon, "Save File", QString::null, | 
|---|
| 210 | this, SLOT(save()), fileTools, "save file" ); | 
|---|
| 211 |  | 
|---|
| 212 | printIcon = QPixmap( fileprint ); | 
|---|
| 213 | QToolButton * filePrint | 
|---|
| 214 | = new <a href="qtoolbutton.html">QToolButton</a>( printIcon, "Print File", QString::null, | 
|---|
| 215 | this, SLOT(print()), fileTools, "print file" ); | 
|---|
| 216 | </pre> | 
|---|
| 217 | <p> In a similar way we create two more tool buttons in this toolbar, each with | 
|---|
| 218 | appropriate icons and tool-tip text. All three buttons are connected | 
|---|
| 219 | to appropriate slots in this object; for example, the "Print File" button | 
|---|
| 220 | to <a href="#printer">ApplicationWindow::print()</a>. | 
|---|
| 221 | <p> <pre>        (void)QWhatsThis::whatsThisButton( fileTools ); | 
|---|
| 222 | </pre> | 
|---|
| 223 | <p> The fourth button in the toolbar is somewhat peculiar: it's the one that | 
|---|
| 224 | provides "What's This?" help. This must be set up using a special | 
|---|
| 225 | function, as its mouse interface is unusual. | 
|---|
| 226 | <p> <pre>        const char * fileOpenText = "<p><img source=\"fileopen\"> " | 
|---|
| 227 | "Click this button to open a <em>new file</em>.<br>" | 
|---|
| 228 | "You can also select the <b>Open</b> command " | 
|---|
| 229 | "from the <b>File</b> menu.</p>"; | 
|---|
| 230 |  | 
|---|
| 231 | QWhatsThis::<a href="qwhatsthis.html#add">add</a>( fileOpen, fileOpenText ); | 
|---|
| 232 | </pre> | 
|---|
| 233 | <p> With the above line we add the "What's This?" help-text to the | 
|---|
| 234 | <em>fileOpen</em> button... | 
|---|
| 235 | <p> <pre>        QMimeSourceFactory::<a href="qmimesourcefactory.html#defaultFactory">defaultFactory</a>()->setPixmap( "fileopen", openIcon ); | 
|---|
| 236 | </pre> | 
|---|
| 237 | <p> ... and tell the rich-text engine that when a help-text (like the one | 
|---|
| 238 | saved in <em>fileOpenText</em>) requests an image named "fileopen", the <em>openIcon</em> pixmap is used. | 
|---|
| 239 | <p> <pre>        const char * fileSaveText = "<p>Click this button to save the file you " | 
|---|
| 240 | "are editing. You will be prompted for a file name.\n" | 
|---|
| 241 | "You can also select the <b>Save</b> command " | 
|---|
| 242 | "from the <b>File</b> menu.</p>"; | 
|---|
| 243 |  | 
|---|
| 244 | QWhatsThis::<a href="qwhatsthis.html#add">add</a>( fileSave, fileSaveText ); | 
|---|
| 245 | const char * filePrintText = "Click this button to print the file you " | 
|---|
| 246 | "are editing.\n" | 
|---|
| 247 | "You can also select the Print command " | 
|---|
| 248 | "from the File menu."; | 
|---|
| 249 |  | 
|---|
| 250 | QWhatsThis::<a href="qwhatsthis.html#add">add</a>( filePrint, filePrintText ); | 
|---|
| 251 | </pre> | 
|---|
| 252 | <p> The "What's This?" help of the remaining two buttons doesn't make use | 
|---|
| 253 | of pixmaps, therefore all we need to do is to add the help-text to the | 
|---|
| 254 | button. Be careful though: To invoke the rich-text elements in <tt>fileSaveText()</tt>, the entire string must be surrounded by <p> and | 
|---|
| 255 | </p>. In <tt>filePrintText()</tt>, we don't have rich-text elements, so | 
|---|
| 256 | this is not necessary. | 
|---|
| 257 | <p> <pre>        <a href="qpopupmenu.html">QPopupMenu</a> * file = new <a href="qpopupmenu.html">QPopupMenu</a>( this ); | 
|---|
| 258 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( "&File", file ); | 
|---|
| 259 | </pre> | 
|---|
| 260 | <p> Next we create a <a href="qpopupmenu.html">QPopupMenu</a> for the <em>File</em> menu and | 
|---|
| 261 | add it to the menu bar. With the ampersand in front of the letter F, | 
|---|
| 262 | we allow the user to use the shortcut <em>Alt+F</em> to pop up this menu. | 
|---|
| 263 | <p> <pre>        file-><a href="qmenudata.html#insertItem">insertItem</a>( "&New", this, SLOT(newDoc()), CTRL+Key_N ); | 
|---|
| 264 | </pre> | 
|---|
| 265 | <p> Its first entry is connected to the (yet to be implemented) slot <tt>newDoc()</tt>. When the user chooses this <em>New</em> entry (e.g. by typing the | 
|---|
| 266 | letter N as marked by the ampersand) or uses the | 
|---|
| 267 | <em>Ctrl+N</em> accelerator, a new editor-window will pop up. | 
|---|
| 268 | <p> <pre>        int id; | 
|---|
| 269 | id = file-><a href="qmenudata.html#insertItem">insertItem</a>( openIcon, "&Open...", | 
|---|
| 270 | this, SLOT(choose()), CTRL+Key_O ); | 
|---|
| 271 | file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileOpenText ); | 
|---|
| 272 |  | 
|---|
| 273 | id = file-><a href="qmenudata.html#insertItem">insertItem</a>( saveIcon, "&Save", | 
|---|
| 274 | this, SLOT(save()), CTRL+Key_S ); | 
|---|
| 275 | file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText ); | 
|---|
| 276 |  | 
|---|
| 277 | id = file-><a href="qmenudata.html#insertItem">insertItem</a>( "Save &As...", this, SLOT(saveAs()) ); | 
|---|
| 278 | file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText ); | 
|---|
| 279 | </pre> | 
|---|
| 280 | <p> We populate the <em>File</em> menu with three more commands (<em>Open</em>, <em>Save</em> and | 
|---|
| 281 | <em>Save As</em>), and set "What's This?" help for them. Note in particular | 
|---|
| 282 | that "What's This?" help and pixmaps are used in both the toolbar (above) | 
|---|
| 283 | and the menu bar (here). (See <a href="qaction.html">QAction</a> and the <tt>examples/action</tt> | 
|---|
| 284 | example for a shorter and easier approach.) | 
|---|
| 285 | <p> <pre>        file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>(); | 
|---|
| 286 | </pre> | 
|---|
| 287 | <p> Then we insert a separator, ... | 
|---|
| 288 | <p> <pre>        id = file-><a href="qmenudata.html#insertItem">insertItem</a>( printIcon, "&Print...", | 
|---|
| 289 | this, SLOT(print()), CTRL+Key_P ); | 
|---|
| 290 | file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, filePrintText ); | 
|---|
| 291 |  | 
|---|
| 292 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>(); | 
|---|
| 293 |  | 
|---|
| 294 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Close", this, SLOT(<a href="qwidget.html#close">close</a>()), CTRL+Key_W ); | 
|---|
| 295 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Quit", qApp, SLOT( <a href="qapplication.html#closeAllWindows">closeAllWindows</a>() ), CTRL+Key_Q ); | 
|---|
| 296 | </pre> | 
|---|
| 297 | <p> ... the <em>Print</em> command with "What's This?" help, another separator and | 
|---|
| 298 | two more commands (<em>Close</em> and <em>Quit</em>) without "What's This?" and pixmaps. | 
|---|
| 299 | In case of the <em>Close</em> command, the signal is connected | 
|---|
| 300 | to the <em>close()</em> slot of the respective <em>ApplicationWindow</em> object whilst | 
|---|
| 301 | the <em>Quit</em> command affects the entire application. | 
|---|
| 302 | <p> Because <em>ApplicationWindow</em> is a <a href="qwidget.html">QWidget</a>, the <em>close()</em> function | 
|---|
| 303 | triggers a call to <a href="#closeEvent">closeEvent()</a> which we | 
|---|
| 304 | will implement later. | 
|---|
| 305 | <p> <a name="common_constructor"></a> | 
|---|
| 306 | <pre>        <a href="qmainwindow.html#menuBar">menuBar</a>()->insertSeparator(); | 
|---|
| 307 | </pre> | 
|---|
| 308 | <p> Now that we have done the File menu we shift our focus back to the | 
|---|
| 309 | menu bar and insert a separator. From now on further menu bar entries | 
|---|
| 310 | will be aligned to the right if the windows system style requires it. | 
|---|
| 311 | <p> <pre>        <a href="qpopupmenu.html">QPopupMenu</a> * help = new <a href="qpopupmenu.html">QPopupMenu</a>( this ); | 
|---|
| 312 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( "&Help", help ); | 
|---|
| 313 |  | 
|---|
| 314 | help-><a href="qmenudata.html#insertItem">insertItem</a>( "&About", this, SLOT(about()), Key_F1 ); | 
|---|
| 315 | help-><a href="qmenudata.html#insertItem">insertItem</a>( "About &Qt", this, SLOT(aboutQt()) ); | 
|---|
| 316 | help-><a href="qmenudata.html#insertSeparator">insertSeparator</a>(); | 
|---|
| 317 | help-><a href="qmenudata.html#insertItem">insertItem</a>( "What's &This", this, SLOT(<a href="qmainwindow.html#whatsThis">whatsThis</a>()), SHIFT+Key_F1 ); | 
|---|
| 318 | </pre> | 
|---|
| 319 | <p> We create a <em>Help</em> menu, add it to the menu bar, and insert a few | 
|---|
| 320 | commands. Depending on the style it will appear on the right hand | 
|---|
| 321 | side of the menu bar or not. | 
|---|
| 322 | <p> <pre>        e = new <a href="qtextedit.html">QTextEdit</a>( this, "editor" ); | 
|---|
| 323 | e-><a href="qwidget.html#setFocus">setFocus</a>(); | 
|---|
| 324 | <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( e ); | 
|---|
| 325 | </pre> | 
|---|
| 326 | <p> Now we create a simple text-editor, set the initial focus to it, | 
|---|
| 327 | and make it the window's central widget. | 
|---|
| 328 | <p> <a href="qmainwindow.html#centralWidget">QMainWindow::centralWidget</a>() is the heart of the entire application: | 
|---|
| 329 | It's what menu bar, statusbar and toolbars are all arranged around. Since | 
|---|
| 330 | the central widget is a text editing widget, we can now reveal that | 
|---|
| 331 | our simple application is a text editor. :) | 
|---|
| 332 | <p> <pre>        <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Ready", 2000 ); | 
|---|
| 333 | </pre> | 
|---|
| 334 | <p> We make the statusbar say "Ready" for two seconds at startup, just to | 
|---|
| 335 | tell the user that the window has finished initialization and can be | 
|---|
| 336 | used. | 
|---|
| 337 | <p> <pre>        <a href="qwidget.html#resize">resize</a>( 450, 600 ); | 
|---|
| 338 | </pre> | 
|---|
| 339 | <p> Finally it's time to resize the new window to a a nice default size. | 
|---|
| 340 | <p> <pre>    } | 
|---|
| 341 | </pre> | 
|---|
| 342 | <p> We have now finished with the constructor. Now we'll deal with the | 
|---|
| 343 | destructor. | 
|---|
| 344 | <p> <pre>    ApplicationWindow::~ApplicationWindow() | 
|---|
| 345 | { | 
|---|
| 346 | delete printer; | 
|---|
| 347 | } | 
|---|
| 348 | </pre> | 
|---|
| 349 | <p> The only thing an <em>ApplicationWindow</em> widget needs to do in its | 
|---|
| 350 | destructor is  to delete the printer it created. All other objects are | 
|---|
| 351 | child widgets, which Qt will delete when appropriate. | 
|---|
| 352 | <p> Now our task is to implement all the slots mentioned in the header file | 
|---|
| 353 | and used in the constructor. | 
|---|
| 354 | <p> <a name="newDoc()"></a> | 
|---|
| 355 | <pre>    void ApplicationWindow::newDoc() | 
|---|
| 356 | { | 
|---|
| 357 | ApplicationWindow *ed = new ApplicationWindow; | 
|---|
| 358 | ed-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Application"); | 
|---|
| 359 | ed-><a href="qwidget.html#show">show</a>(); | 
|---|
| 360 | } | 
|---|
| 361 | </pre> | 
|---|
| 362 | <p> This slot, connected to the <em>File|New</em> menu item, simply creates a | 
|---|
| 363 | new <em>ApplicationWindow</em> and shows it. | 
|---|
| 364 | <p> <a name="choose()"></a> | 
|---|
| 365 | <pre>    void ApplicationWindow::choose() | 
|---|
| 366 | { | 
|---|
| 367 | <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, QString::null, | 
|---|
| 368 | this); | 
|---|
| 369 | if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) | 
|---|
| 370 | load( fn ); | 
|---|
| 371 | else | 
|---|
| 372 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Loading aborted", 2000 ); | 
|---|
| 373 | } | 
|---|
| 374 | </pre> | 
|---|
| 375 | <p> The <em>choose()</em> slot is connected to the <em>Open</em> menu item and | 
|---|
| 376 | tool button. With a little help from <a href="qfiledialog.html#getOpenFileName">QFileDialog::getOpenFileName</a>(), it | 
|---|
| 377 | asks the user for a file name and then either loads that file or gives an | 
|---|
| 378 | error message in the statusbar. | 
|---|
| 379 | <p> <pre>    void ApplicationWindow::load( const <a href="qstring.html">QString</a> &fileName ) | 
|---|
| 380 | { | 
|---|
| 381 | <a href="qfile.html">QFile</a> f( fileName ); | 
|---|
| 382 | if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) ) | 
|---|
| 383 | return; | 
|---|
| 384 |  | 
|---|
| 385 | <a href="qtextstream.html">QTextStream</a> ts( &f ); | 
|---|
| 386 | e-><a href="qtextedit.html#setText">setText</a>( ts.<a href="qtextstream.html#read">read</a>() ); | 
|---|
| 387 | e-><a href="qtextedit.html#setModified">setModified</a>( FALSE ); | 
|---|
| 388 | <a href="qwidget.html#setCaption">setCaption</a>( fileName ); | 
|---|
| 389 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Loaded document " + fileName, 2000 ); | 
|---|
| 390 | } | 
|---|
| 391 | </pre> | 
|---|
| 392 | <p> This function loads a file into the editor. When it's done, it sets the | 
|---|
| 393 | window system caption to the file name and displays a success message in | 
|---|
| 394 | the statusbar for two seconds. With files that exist but are not | 
|---|
| 395 | readable, nothing happens. | 
|---|
| 396 | <p> <a name="save()"></a> | 
|---|
| 397 | <pre>    void ApplicationWindow::save() | 
|---|
| 398 | { | 
|---|
| 399 | if ( filename.isEmpty() ) { | 
|---|
| 400 | saveAs(); | 
|---|
| 401 | return; | 
|---|
| 402 | } | 
|---|
| 403 |  | 
|---|
| 404 | <a href="qstring.html">QString</a> text = e-><a href="qtextedit.html#text">text</a>(); | 
|---|
| 405 | <a href="qfile.html">QFile</a> f( filename ); | 
|---|
| 406 | if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) ) { | 
|---|
| 407 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( QString("Could not write to %1").arg(filename), | 
|---|
| 408 | 2000 ); | 
|---|
| 409 | return; | 
|---|
| 410 | } | 
|---|
| 411 |  | 
|---|
| 412 | <a href="qtextstream.html">QTextStream</a> t( &f ); | 
|---|
| 413 | t << text; | 
|---|
| 414 | f.<a href="qfile.html#close">close</a>(); | 
|---|
| 415 | </pre> | 
|---|
| 416 | <p> As its name suggests, this function saves the current file. If no | 
|---|
| 417 | filename has been specified so far, the <a href="#saveAs()">saveAs()</a> function is called. Unwritable files cause the <em>ApplicationWindow</em> object to provide an error-message in the statusbar. | 
|---|
| 418 | Note that there is more than one way to do this: | 
|---|
| 419 | compare the above <tt>statusBar()->message()</tt> line with the equivalent | 
|---|
| 420 | code in the <tt>load()</tt> function. | 
|---|
| 421 | <p> <pre>        e-><a href="qtextedit.html#setModified">setModified</a>( FALSE ); | 
|---|
| 422 | </pre> | 
|---|
| 423 | <p> Tell the editor that the contents haven't been edited since the last | 
|---|
| 424 | save. When the user does some further editing and wishes to close the | 
|---|
| 425 | window without explicit saving, <a href="#closeEvent">ApplicationWindow::closeEvent()</a> will ask about it. | 
|---|
| 426 | <p> <pre>        <a href="qwidget.html#setCaption">setCaption</a>( filename ); | 
|---|
| 427 | </pre> | 
|---|
| 428 | <p> It may be that the document was saved under a different name than the | 
|---|
| 429 | old caption suggests, so we set the window caption just to be sure. | 
|---|
| 430 | <p> <pre>        <a href="qmainwindow.html#statusBar">statusBar</a>()->message( QString( "File %1 saved" ).arg( filename ), 2000 ); | 
|---|
| 431 | } | 
|---|
| 432 | </pre> | 
|---|
| 433 | <p> With a message in the statusbar, we inform the user that the file | 
|---|
| 434 | was saved successfully. | 
|---|
| 435 | <p> <a name="saveAs()"></a> | 
|---|
| 436 | <pre>    void ApplicationWindow::saveAs() | 
|---|
| 437 | { | 
|---|
| 438 | <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>( QString::null, QString::null, | 
|---|
| 439 | this ); | 
|---|
| 440 | if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) { | 
|---|
| 441 | filename = fn; | 
|---|
| 442 | save(); | 
|---|
| 443 | } else { | 
|---|
| 444 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Saving aborted", 2000 ); | 
|---|
| 445 | } | 
|---|
| 446 | } | 
|---|
| 447 | </pre> | 
|---|
| 448 | <p> This function asks for a new name, saves the document under that name, | 
|---|
| 449 | and implicitly changes the window system caption to the new name. | 
|---|
| 450 | <p> <a name="printer"></a> | 
|---|
| 451 | <p> <pre>    void ApplicationWindow::print() | 
|---|
| 452 | { | 
|---|
| 453 | printer-><a href="qprinter.html#setFullPage">setFullPage</a>( TRUE ); | 
|---|
| 454 | if ( printer-><a href="qprinter.html#setup">setup</a>(this) ) {               // printer dialog | 
|---|
| 455 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing..." ); | 
|---|
| 456 | <a href="qpainter.html">QPainter</a> p; | 
|---|
| 457 | if( !p.<a href="qpainter.html#begin">begin</a>( printer ) ) {               // paint on printer | 
|---|
| 458 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing aborted", 2000 ); | 
|---|
| 459 | return; | 
|---|
| 460 | } | 
|---|
| 461 |  | 
|---|
| 462 | <a href="qpaintdevicemetrics.html">QPaintDeviceMetrics</a> metrics( p.<a href="qpainter.html#device">device</a>() ); | 
|---|
| 463 | int dpiy = metrics.<a href="qpaintdevicemetrics.html#logicalDpiY">logicalDpiY</a>(); | 
|---|
| 464 | int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins | 
|---|
| 465 | <a href="qrect.html">QRect</a> view( margin, margin, metrics.<a href="qpaintdevicemetrics.html#width">width</a>() - 2*margin, metrics.<a href="qpaintdevicemetrics.html#height">height</a>() - 2*margin ); | 
|---|
| 466 | <a href="qsimplerichtext.html">QSimpleRichText</a> richText( QStyleSheet::<a href="qstylesheet.html#convertFromPlainText">convertFromPlainText</a>(e-><a href="qtextedit.html#text">text</a>()), | 
|---|
| 467 | QFont(), | 
|---|
| 468 | e-><a href="qtextedit.html#context">context</a>(), | 
|---|
| 469 | e-><a href="qtextedit.html#styleSheet">styleSheet</a>(), | 
|---|
| 470 | e-><a href="qtextedit.html#mimeSourceFactory">mimeSourceFactory</a>(), | 
|---|
| 471 | view.<a href="qrect.html#height">height</a>() ); | 
|---|
| 472 | richText.<a href="qsimplerichtext.html#setWidth">setWidth</a>( &p, view.<a href="qrect.html#width">width</a>() ); | 
|---|
| 473 | int page = 1; | 
|---|
| 474 | do { | 
|---|
| 475 | richText.<a href="qsimplerichtext.html#draw">draw</a>( &p, margin, margin, view, colorGroup() ); | 
|---|
| 476 | view.<a href="qrect.html#moveBy">moveBy</a>( 0, view.<a href="qrect.html#height">height</a>() ); | 
|---|
| 477 | p.<a href="qpainter.html#translate">translate</a>( 0 , -view.<a href="qrect.html#height">height</a>() ); | 
|---|
| 478 | p.<a href="qpainter.html#drawText">drawText</a>( view.<a href="qrect.html#right">right</a>() - p.<a href="qpainter.html#fontMetrics">fontMetrics</a>().width( QString::<a href="qstring.html#number">number</a>( page ) ), | 
|---|
| 479 | view.<a href="qrect.html#bottom">bottom</a>() + p.<a href="qpainter.html#fontMetrics">fontMetrics</a>().ascent() + 5, QString::number( page ) ); | 
|---|
| 480 | if ( view.<a href="qrect.html#top">top</a>() - margin >= richText.<a href="qsimplerichtext.html#height">height</a>() ) | 
|---|
| 481 | break; | 
|---|
| 482 | printer-><a href="qprinter.html#newPage">newPage</a>(); | 
|---|
| 483 | page++; | 
|---|
| 484 | } while (TRUE); | 
|---|
| 485 |  | 
|---|
| 486 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing completed", 2000 ); | 
|---|
| 487 | } else { | 
|---|
| 488 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing aborted", 2000 ); | 
|---|
| 489 | } | 
|---|
| 490 | } | 
|---|
| 491 | </pre> | 
|---|
| 492 | <p> <em>print()</em> is called by the <em>File|Print</em> menu item and the <em>filePrint</em> | 
|---|
| 493 | tool button. | 
|---|
| 494 | <p> We present the user with the print setup dialog, and abandon printing | 
|---|
| 495 | if they cancel. | 
|---|
| 496 | <p> We create a <a href="qsimplerichtext.html">QSimpleRichText</a> object and give it the text. This object | 
|---|
| 497 | is able to format the text nicely as one long page. We achieve | 
|---|
| 498 | pagination by printing one paper page's worth of text from the | 
|---|
| 499 | QSimpleRichText page at a time. | 
|---|
| 500 | <p> Now let's see what happens when a user wishes to <em>close()</em> | 
|---|
| 501 | an <em>ApplicationWindow</em>. | 
|---|
| 502 | <p> <a name="closeEvent"></a> | 
|---|
| 503 | <pre>    void ApplicationWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a>* ce ) | 
|---|
| 504 | { | 
|---|
| 505 | </pre> | 
|---|
| 506 | <p> This event gets to process window system close events. A close event is | 
|---|
| 507 | subtly different from a hide event: hide often means "iconify" whereas | 
|---|
| 508 | close means that the window is going away for good. | 
|---|
| 509 | <p> <pre>        if ( !e-><a href="qtextedit.html#isModified">isModified</a>() ) { | 
|---|
| 510 | ce-><a href="qcloseevent.html#accept">accept</a>(); | 
|---|
| 511 | return; | 
|---|
| 512 | } | 
|---|
| 513 | </pre> | 
|---|
| 514 | <p> If the text hasn't been edited, we just accept the event. The window | 
|---|
| 515 | will be closed, and because we used the <em>WDestructiveClose</em> <a href="qt.html#WidgetFlags">widget flag</a> in the <a href="#ApplicationWindow"><i>ApplicationWindow()</i> constructor</a>, | 
|---|
| 516 | the widget will be deleted. | 
|---|
| 517 | <p> <pre>        switch( QMessageBox::<a href="qmessagebox.html#information">information</a>( this, "Qt Application Example", | 
|---|
| 518 | "Do you want to save the changes" | 
|---|
| 519 | " to the document?", | 
|---|
| 520 | "Yes", "No", "Cancel", | 
|---|
| 521 | 0, 1 ) ) { | 
|---|
| 522 | </pre> | 
|---|
| 523 | <p> Otherwise we ask the user: What do you want to do? | 
|---|
| 524 | <p> <pre>        case 0: | 
|---|
| 525 | save(); | 
|---|
| 526 | ce-><a href="qcloseevent.html#accept">accept</a>(); | 
|---|
| 527 | break; | 
|---|
| 528 | </pre> | 
|---|
| 529 | <p> If they want to save and then exit, we do that. | 
|---|
| 530 | <p> <pre>        case 1: | 
|---|
| 531 | ce-><a href="qcloseevent.html#accept">accept</a>(); | 
|---|
| 532 | break; | 
|---|
| 533 | </pre> | 
|---|
| 534 | <p> If the user doesn't want to exit, we ignore the close event (there is | 
|---|
| 535 | a chance that we can't block it but we try). | 
|---|
| 536 | <p> <pre>        case 2: | 
|---|
| 537 | default: // just for sanity | 
|---|
| 538 | ce-><a href="qcloseevent.html#ignore">ignore</a>(); | 
|---|
| 539 | break; | 
|---|
| 540 | </pre> | 
|---|
| 541 | <p> The last case -- the user wants to abandon the edits and exit -- is very | 
|---|
| 542 | simple. | 
|---|
| 543 | <p> <pre>        } | 
|---|
| 544 | } | 
|---|
| 545 | </pre> | 
|---|
| 546 | <p> Last but not least we implement the slots used by the help menu entries. | 
|---|
| 547 | <p> <pre>    void ApplicationWindow::about() | 
|---|
| 548 | { | 
|---|
| 549 | QMessageBox::<a href="qmessagebox.html#about">about</a>( this, "Qt Application Example", | 
|---|
| 550 | "This example demonstrates simple use of " | 
|---|
| 551 | "QMainWindow,\nQMenuBar and QToolBar."); | 
|---|
| 552 | } | 
|---|
| 553 |  | 
|---|
| 554 | void ApplicationWindow::aboutQt() | 
|---|
| 555 | { | 
|---|
| 556 | QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Qt Application Example" ); | 
|---|
| 557 | } | 
|---|
| 558 | </pre> | 
|---|
| 559 | <p> These two slots use ready-made "about" functions to provide some | 
|---|
| 560 | information about this program and the GUI toolkit it uses. (Although you | 
|---|
| 561 | don't need to provide an About Qt in your programs, if you use Qt for free | 
|---|
| 562 | we would appreciate it if you tell people what you're using.) | 
|---|
| 563 | <p> That was all we needed to write a complete, almost useful application with | 
|---|
| 564 | nice help-functions, almost as good as the "editors" some computer vendors | 
|---|
| 565 | ship with their desktops, and in less than 300 lines of code! | 
|---|
| 566 | <p> <p>See also <a href="step-by-step-examples.html">Step-by-step Examples</a>. | 
|---|
| 567 |  | 
|---|
| 568 | <!-- eof --> | 
|---|
| 569 | <p><address><hr><div align=center> | 
|---|
| 570 | <table width=100% cellspacing=0 border=0><tr> | 
|---|
| 571 | <td>Copyright © 2007 | 
|---|
| 572 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> | 
|---|
| 573 | <td align=right><div align=right>Qt 3.3.8</div> | 
|---|
| 574 | </table></div></address></body> | 
|---|
| 575 | </html> | 
|---|