File: application-main-cpp.html

package info (click to toggle)
qt-x11 3%3A2.3.1-22
  • links: PTS
  • area: main
  • in suites: woody
  • size: 48,524 kB
  • ctags: 46,337
  • sloc: cpp: 260,077; ansic: 32,457; makefile: 31,131; yacc: 2,444; sh: 1,513; lex: 480; perl: 422; xml: 68; lisp: 15
file content (399 lines) | stat: -rw-r--r-- 17,403 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
Qt Toolkit - application/main.cpp example file
</title><style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">

<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>

<h1 align=center>A Complete Application Window</h1><br clear="all">

  This example program looks like a complete modern application.  It
  has a menu bar, it has a tool bar, it has a status bar and works
  like a simple text editor.

  There is a <a href="simple-application.html">walkthrough</a> of
  this example.

  <hr>

  Header file: <pre>/****************************************************************************
** &#36;Id&#58; qt/examples/application/application.h   2.3.1   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef APPLICATION_H
#define APPLICATION_H

#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;

class QMultiLineEdit;
class QToolBar;
class QPopupMenu;

class ApplicationWindow: public QMainWindow
{
    Q_OBJECT
public:
    ApplicationWindow();
    ~ApplicationWindow();

protected:
    void closeEvent( <a href="qcloseevent.html">QCloseEvent</a>* );

private slots:
    void newDoc();
    void load();
    void load( const char *fileName );
    void save();
    void saveAs();
    void print();

    void about();
    void aboutQt();

private:
    <a href="qprinter.html">QPrinter</a> *printer;
    <a href="qmultilineedit.html">QMultiLineEdit</a> *e;
    <a href="qtoolbar.html">QToolBar</a> *fileTools;
    <a href="qstring.html">QString</a> filename;
};

#endif
</pre>


  <hr>

  Implementation: <pre>/****************************************************************************
** &#36;Id&#58; qt/examples/application/application.cpp   2.3.1   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "application.h"

#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
#include &lt;<a href="qtoolbar-h.html">qtoolbar.h</a>&gt;
#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;
#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
#include &lt;qkeycode.h&gt;
#include &lt;<a href="qmultilineedit-h.html">qmultilineedit.h</a>&gt;
#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
#include &lt;<a href="qprinter-h.html">qprinter.h</a>&gt;
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qaccel-h.html">qaccel.h</a>&gt;
#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a href="qpaintdevicemetrics-h.html">qpaintdevicemetrics.h</a>&gt;
#include &lt;<a href="qwhatsthis-h.html">qwhatsthis.h</a>&gt;

#include "filesave.xpm"
#include "fileopen.xpm"
#include "fileprint.xpm"

const char * fileOpenText = "&lt;img source=\"fileopen\"&gt; "
"Click this button to open a &lt;em&gt;new file&lt;/em&gt;. &lt;br&gt;&lt;br&gt;"
"You can also select the &lt;b&gt;Open command&lt;/b&gt; from the File menu.";
const char * fileSaveText = "Click this button to save the file you are "
"editing.  You will be prompted for a file name.\n\n"
"You can also select the Save command from the File menu.\n\n"
"Note that implementing this function is left as an exercise for the reader.";
const char * filePrintText = "Click this button to print the file you "
"are editing.\n\n"
"You can also select the Print command from the File menu.";

ApplicationWindow::ApplicationWindow()
    : <a href="qmainwindow.html">QMainWindow</a>( 0, "example application main window", WDestructiveClose )
{
    int id;

    printer = new <a href="qprinter.html">QPrinter</a>;
    <a href="qpixmap.html">QPixmap</a> openIcon, saveIcon, printIcon;

    fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );
    fileTools-&gt;setLabel( <a href="qobject.html#ba30fb">tr</a>( "File Operations" ) );

    openIcon = QPixmap( fileopen );
    <a href="qtoolbutton.html">QToolButton</a> * fileOpen
        = new <a href="qtoolbutton.html">QToolButton</a>( openIcon, "Open File", QString::null,
                           this, SLOT(<a href=#158>load</a>()), fileTools, "open file" );

    saveIcon = QPixmap( filesave );
    <a href="qtoolbutton.html">QToolButton</a> * fileSave
        = new <a href="qtoolbutton.html">QToolButton</a>( saveIcon, "Save File", QString::null,
                           this, SLOT(<a href=#159>save</a>()), fileTools, "save file" );

    printIcon = QPixmap( fileprint );
    <a href="qtoolbutton.html">QToolButton</a> * filePrint
        = new <a href="qtoolbutton.html">QToolButton</a>( printIcon, "Print File", QString::null,
                           this, SLOT(<a href=#161>print</a>()), fileTools, "print file" );

    (void)QWhatsThis::whatsThisButton( fileTools );

    <a href="qwhatsthis.html#5265e7">QWhatsThis::add</a>( fileOpen, fileOpenText );
    <a href="qmimesourcefactory.html#28c4d9">QMimeSourceFactory::defaultFactory</a>()-&gt;setPixmap( "fileopen", openIcon );
    <a href="qwhatsthis.html#5265e7">QWhatsThis::add</a>( fileSave, fileSaveText );
    <a href="qwhatsthis.html#5265e7">QWhatsThis::add</a>( filePrint, filePrintText );

    <a href="qpopupmenu.html">QPopupMenu</a> * file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    <a href="qmainwindow.html#2fcbc3">menuBar</a>()-&gt;insertItem( "&amp;File", file );

    file-&gt;<a href="qmenudata.html#43c652">insertItem</a>( "&amp;New", this, SLOT(<a href=#156>newDoc</a>()), CTRL+Key_N );

    id = file-&gt;<a href="qmenudata.html#43c652">insertItem</a>( openIcon, "&amp;Open",
                           this, SLOT(<a href=#158>load</a>()), CTRL+Key_O );
    file-&gt;<a href="qmenudata.html#a678e4">setWhatsThis</a>( id, fileOpenText );

    id = file-&gt;<a href="qmenudata.html#43c652">insertItem</a>( saveIcon, "&amp;Save",
                           this, SLOT(<a href=#159>save</a>()), CTRL+Key_S );
    file-&gt;<a href="qmenudata.html#a678e4">setWhatsThis</a>( id, fileSaveText );
    id = file-&gt;<a href="qmenudata.html#43c652">insertItem</a>( "Save &amp;as...", this, SLOT(<a href=#160>saveAs</a>()) );
    file-&gt;<a href="qmenudata.html#a678e4">setWhatsThis</a>( id, fileSaveText );
    file-&gt;<a href="qmenudata.html#5f2cea">insertSeparator</a>();
    id = file-&gt;<a href="qmenudata.html#43c652">insertItem</a>( printIcon, "&amp;Print",
                           this, SLOT(<a href=#161>print</a>()), CTRL+Key_P );
    file-&gt;<a href="qmenudata.html#a678e4">setWhatsThis</a>( id, filePrintText );
    file-&gt;<a href="qmenudata.html#5f2cea">insertSeparator</a>();
    file-&gt;<a href="qmenudata.html#43c652">insertItem</a>( "&amp;Close", this, SLOT(<a href="qwidget.html#930c34">close</a>()), CTRL+Key_W );
    file-&gt;<a href="qmenudata.html#43c652">insertItem</a>( "&amp;Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q );

    <a href="qpopupmenu.html">QPopupMenu</a> * help = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    <a href="qmainwindow.html#2fcbc3">menuBar</a>()-&gt;insertSeparator();
    <a href="qmainwindow.html#2fcbc3">menuBar</a>()-&gt;insertItem( "&amp;Help", help );

    help-&gt;<a href="qmenudata.html#43c652">insertItem</a>( "&amp;About", this, SLOT(<a href=#163>about</a>()), Key_F1 );
    help-&gt;<a href="qmenudata.html#43c652">insertItem</a>( "About &amp;<a href="qt.html">Qt</a>", this, SLOT(<a href=#164>aboutQt</a>()) );
    help-&gt;<a href="qmenudata.html#5f2cea">insertSeparator</a>();
    help-&gt;<a href="qmenudata.html#43c652">insertItem</a>( "What's &amp;This", this, SLOT(<a href="qmainwindow.html#db02bc">whatsThis</a>()), SHIFT+Key_F1 );

    e = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this, "editor" );
    e-&gt;setFocus();
    <a href="qmainwindow.html#0983b6">setCentralWidget</a>( e );
    <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( "Ready", 2000 );
    <a href="qwidget.html#9ff9d8">resize</a>( 450, 600 );
}

ApplicationWindow::~ApplicationWindow()
{
    delete printer;
}

void <a name="156"></a>ApplicationWindow::newDoc()
{
    ApplicationWindow *ed = new ApplicationWindow;
    ed-&gt;<a href="qmainwindow.html#e71bb3">show</a>();
}

void <a name="158"></a>ApplicationWindow::load()
{
    <a href="qstring.html">QString</a> fn = QFileDialog::getOpenFileName( QString::null, QString::null,
                                               this);
    if ( !fn.<a href="qstring.html#9a6d7e">isEmpty</a>() )
        <a href=#158>load</a>( fn );
    else
        <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( "Loading aborted", 2000 );
}

void <a name="158"></a>ApplicationWindow::load( const char *fileName )
{
    <a href="qfile.html">QFile</a> f( fileName );
    if ( !f.<a href="qfile.html#44ae9b">open</a>( IO_ReadOnly ) )
        return;

    e-&gt;setAutoUpdate( FALSE );
    e-&gt;clear();

    <a href="qtextstream.html">QTextStream</a> t(&amp;f);
    while ( !t.<a href="qtextstream.html#29d3b3">eof</a>() ) {
        <a href="qstring.html">QString</a> s = t.<a href="qtextstream.html#6a73a4">readLine</a>();
        e-&gt;append( s );
    }
    f.<a href="qfile.html#e37a87">close</a>();

    e-&gt;setAutoUpdate( TRUE );
    e-&gt;repaint();
    e-&gt;setEdited( FALSE );
    <a href="qwidget.html#d4f007">setCaption</a>( fileName );
    <a href="qstring.html">QString</a> s;
    s.<a href="qstring.html#2eb9bc">sprintf</a>( "Loaded document %s", fileName );
    <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( s, 2000 );
}

void <a name="159"></a>ApplicationWindow::save()
{
    if ( filename.isEmpty() ) {
        <a href=#160>saveAs</a>();
        return;
    }

    <a href="qstring.html">QString</a> text = e-&gt;text();
    <a href="qfile.html">QFile</a> f( filename );
    if ( !f.<a href="qfile.html#44ae9b">open</a>( IO_WriteOnly ) ) {
        <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( <a href="qstring.html">QString</a>("Could not write to %1").arg(filename),
                              2000 );
        return;
    }

    <a href="qtextstream.html">QTextStream</a> t( &amp;f );
    t &lt;&lt; text;
    f.<a href="qfile.html#e37a87">close</a>();

    e-&gt;setEdited( FALSE );

    <a href="qwidget.html#d4f007">setCaption</a>( filename );

    <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( <a href="qstring.html">QString</a>( "File %1 saved" ).arg( filename ), 2000 );
}

void <a name="160"></a>ApplicationWindow::saveAs()
{
    <a href="qstring.html">QString</a> fn = QFileDialog::getSaveFileName( QString::null, QString::null,
                                               this );
    if ( !fn.<a href="qstring.html#9a6d7e">isEmpty</a>() ) {
        filename = fn;
        <a href=#159>save</a>();
    } else {
        <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( "Saving aborted", 2000 );
    }
}

void <a name="161"></a>ApplicationWindow::print()
{
    const int Margin = 10;
    int pageNo = 1;

    if ( printer-&gt;setup(this) ) {               // printer dialog
        <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( "Printing..." );
        <a href="qpainter.html">QPainter</a> p;
        if( !p.<a href="qpainter.html#59f1bb">begin</a>( printer ) )
            return;                             // paint on printer

        p.<a href="qpainter.html#e4d6b7">setFont</a>( e-&gt;font() );
        int yPos        = 0;                    // y position for each line
        <a href="qfontmetrics.html">QFontMetrics</a> fm = p.<a href="qpainter.html#f572a5">fontMetrics</a>();
        <a href="qpaintdevicemetrics.html">QPaintDeviceMetrics</a> metrics( printer ); // need width/height
                                                // of printer surface
        for( int i = 0 ; i &lt; e-&gt;numLines() ; i++ ) {
            if ( Margin + yPos &gt; metrics.<a href="qpaintdevicemetrics.html#3792df">height</a>() - Margin ) {
                <a href="qstring.html">QString</a> msg( "Printing (page " );
                msg += QString::number( ++pageNo );
                msg += ")...";
                <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( msg );
                printer-&gt;newPage();             // no more room on this page
                yPos = 0;                       // back to top of page
            }
            p.<a href="qpainter.html#8a59ed">drawText</a>( Margin, Margin + yPos,
                        metrics.<a href="qpaintdevicemetrics.html#a53b11">width</a>(), fm.<a href="qfontmetrics.html#ebe056">lineSpacing</a>(),
                        ExpandTabs | DontClip,
                        e-&gt;textLine( i ) );
            yPos = yPos + fm.<a href="qfontmetrics.html#ebe056">lineSpacing</a>();
        }
        p.<a href="qpainter.html#c7f9ac">end</a>();                                // send job to printer
        <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( "Printing completed", 2000 );
    } else {
        <a href="qmainwindow.html#14153d">statusBar</a>()-&gt;message( "Printing aborted", 2000 );
    }
}

void <a name="162"></a>ApplicationWindow::closeEvent( <a href="qcloseevent.html">QCloseEvent</a>* ce )
{
    if ( !e-&gt;edited() ) {
        ce-&gt;<a href="qcloseevent.html#47cc57">accept</a>();
        return;
    }

    switch( <a href="qmessagebox.html#2e454b">QMessageBox::information</a>( this, "Qt Application Example",
                                      "The document has been changed since "
                                      "the last save.",
                                      "Save Now", "Cancel", "Leave Anyway",
                                      0, 1 ) ) {
    case 0:
        <a href=#159>save</a>();
        ce-&gt;<a href="qcloseevent.html#47cc57">accept</a>();
        break;
    case 1:
    default: // just for sanity
        ce-&gt;<a href="qcloseevent.html#e2007a">ignore</a>();
        break;
    case 2:
        ce-&gt;<a href="qcloseevent.html#47cc57">accept</a>();
        break;
    }
}

void <a name="163"></a>ApplicationWindow::about()
{
    <a href="qmessagebox.html#e67950">QMessageBox::about</a>( this, "Qt Application Example",
                        "This example demonstrates simple use of "
                        "QMainWindow,\nQMenuBar and QToolBar.");
}

void <a name="164"></a>ApplicationWindow::aboutQt()
{
    <a href="qmessagebox.html#976755">QMessageBox::aboutQt</a>( this, "Qt Application Example" );
}
</pre>


  <hr>

  Main:
<pre>/****************************************************************************
** &#36;Id&#58; qt/examples/application/main.cpp   2.3.1   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include &lt;<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>&gt;
#include "application.h"

int main( int argc, char ** argv ) {
    <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv );
    ApplicationWindow * mw = new ApplicationWindow();
    mw-&gt;<a name="setCaption"></a><a href="qwidget.html#d4f007">setCaption</a>( "Document 1" );
    mw-&gt;<a name="show"></a><a href="qmainwindow.html#e71bb3">show</a>();
    a.<a name="connect"></a><a href="qobject.html#c2d209">connect</a>( &amp;a, SIGNAL(lastWindowClosed()), &amp;a, SLOT(quit()) );
    return a.<a name="exec"></a><a href="qapplication.html#7076af">exec</a>();
}
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright  2000 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.1</div>
</table></div></address></body></html>