summaryrefslogtreecommitdiffstats
path: root/doc/html/simple-application.html
blob: e9f95182214a978db181a1ae1ed7793415b11a32 (plain)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/application-walkthrough.doc:36 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Walkthrough: A Simple Application</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>



<p> 
<p> This walkthrough shows simple use of <a href="tqmainwindow.html">TQMainWindow</a>, <a href="tqmenubar.html">TQMenuBar</a>, <a href="tqpopupmenu.html">TQPopupMenu</a>, <a href="tqtoolbar.html">TQToolBar</a> and <a href="tqstatusbar.html">TQStatusBar</a> - classes that every
modern application window tends to use. (See also <a href="tutorial2.html">Tutorial #2</a>.)
<p> It also illustrates some aspects of <a href="ntqwhatsthis.html">TQWhatsThis</a> (for simple help) and a
typical <tt>main()</tt> using <a href="ntqapplication.html">TQApplication</a>.
<p> Finally, it shows a typical print function based on <a href="ntqprinter.html">TQPrinter</a>.
<p> <h2> The declaration of ApplicationWindow
</h2>
<a name="1"></a><p> Here's the header file in full:
<p> <pre>/****************************************************************************
** $Id: qt/application.h   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef APPLICATION_H
#define APPLICATION_H

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

class TQTextEdit;

class ApplicationWindow: public <a href="tqmainwindow.html">TQMainWindow</a>
{
    <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>

public:
    ApplicationWindow();
    ~ApplicationWindow();

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

private slots:
    void newDoc();
    void choose();
    void load( const <a href="tqstring.html">TQString</a> &amp;fileName );
    void save();
    void saveAs();
    void print();

    void about();
    void aboutTQt();

private:
    <a href="ntqprinter.html">TQPrinter</a> *printer;
    <a href="tqtextedit.html">TQTextEdit</a> *e;
    <a href="tqstring.html">TQString</a> filename;
};


#endif
</pre>

<p> It declares a class that inherits <a href="tqmainwindow.html">TQMainWindow</a>, with slots and private
variables. The class pre-declaration of <a href="tqtextedit.html">TQTextEdit</a> at the beginning
(instead of an include) helps to speed up compilation. With this
trick, <tt>make depend</tt> won't insist on recompiling every <tt>.cpp</tt> file that
includes <tt>application.h</tt> when <a href="tqtextedit-h.html">tqtextedit.h</a> changes.
<p> <a name="simplemain"></a>
<h2> A simple main()
</h2>
<a name="2"></a><p> Here is <tt>main.cpp</tt> in full:
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

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

int main( int argc, char ** argv ) {
    <a href="ntqapplication.html">TQApplication</a> a( argc, argv );
    ApplicationWindow *mw = new ApplicationWindow();
    mw-&gt;<a href="tqwidget.html#setCaption">setCaption</a>( "TQt Example - Application" );
<a name="x1598"></a>    mw-&gt;<a href="tqwidget.html#show">show</a>();
<a name="x1599"></a>    a.<a href="tqobject.html#connect">connect</a>( &amp;a, TQ_SIGNAL(<a href="ntqapplication.html#lastWindowClosed">lastWindowClosed</a>()), &amp;a, TQ_SLOT(<a href="ntqapplication.html#quit">quit</a>()) );
    return a.<a href="ntqapplication.html#exec">exec</a>();
}
</pre>

<p> Now we'll look at <tt>main.cpp</tt> in detail.
<p> 

<pre>    int main( int argc, char ** argv ) {
        <a href="ntqapplication.html">TQApplication</a> a( argc, argv );
</pre>
<p> With the above line, we create a <a href="ntqapplication.html">TQApplication</a> object with the usual 
constructor and let it
parse <em>argc</em> and <em>argv</em>. TQApplication itself takes care of X11-specific
command-line options like <em>-geometry</em>, so the program will automatically
behave the way X clients are expected to.
<p> <pre>        ApplicationWindow *mw = new ApplicationWindow();
        mw-&gt;<a href="tqwidget.html#setCaption">setCaption</a>( "TQt Example - Application" );
    <a name="x2115"></a>    mw-&gt;<a href="tqwidget.html#show">show</a>();
</pre>
<p> We create an <em>ApplicationWindow</em> as a top-level widget, set its window
system caption to "Document 1", and <em>show()</em> it.
<p> <a name="close"></a>
<pre>        a.<a href="tqobject.html#connect">connect</a>( &amp;a, TQ_SIGNAL(<a href="ntqapplication.html#lastWindowClosed">lastWindowClosed</a>()), &amp;a, TQ_SLOT(<a href="ntqapplication.html#quit">quit</a>()) );
</pre>
<p> When the application's last window is closed, it should quit. Both
the signal and the slot are predefined members of <a href="ntqapplication.html">TQApplication</a>.
<p> <pre>        return a.<a href="ntqapplication.html#exec">exec</a>();
</pre>
<p> Having completed the application's initialization, we start the main
event loop (the GUI), and eventually return the error code
that TQApplication returns when it leaves the event loop.
<p> <pre>    }
</pre>
<p> <a name="ApplicationWindow"></a>
<h2> The Implementation of ApplicationWindow
</h2>
<a name="3"></a><p> 

<p> Since the implementation is quite large (almost 300 lines) we 
won't list the whole thing. (The source code is included in the
examples/application directory.) Before we start with the constructor
there are three <tt>#include</tt>s worth mentioning:
<p> <pre>    #include "filesave.xpm"
    #include "fileopen.xpm"
    #include "fileprint.xpm"
</pre>
<p> The tool buttons in our application wouldn't look good without icons!
These icons can be found in the XPM files included above. If you ever
moved a program to a different location and wondered why icons were
missing afterwards you will probably agree that it is a good idea to
compile them into the binary. This is what we are doing here.
<p> <pre>    ApplicationWindow::ApplicationWindow()
        : <a href="tqmainwindow.html">TQMainWindow</a>( 0, "example application main window", WDestructiveClose | WGroupLeader )
    {
</pre>
<p> <em>ApplicationWindow</em> inherits <a href="tqmainwindow.html">TQMainWindow</a>, the TQt class that provides
typical application main windows, with menu bars, toolbars, etc.
<p> <pre>        printer = new <a href="ntqprinter.html">TQPrinter</a>( TQPrinter::HighResolution );
</pre>
<p> The application example can print things, and we chose to have a
<a href="ntqprinter.html">TQPrinter</a> object lying around so that when the user changes a setting
during one printing, the new setting will be the default next time.
<p> <pre>        <a href="ntqpixmap.html">TQPixmap</a> openIcon, saveIcon, printIcon;
</pre>
<p> For the sake of simplicity, our example only has a few commands in the
toolbar. The above variables are used to hold an icon for each of
them.
<p> <pre>        <a href="tqtoolbar.html">TQToolBar</a> * fileTools = new <a href="tqtoolbar.html">TQToolBar</a>( this, "file operations" );
</pre>
<p> We create a toolbar in <em>this</em> window ...
<p> <pre>        fileTools-&gt;<a href="tqtoolbar.html#setLabel">setLabel</a>( "File Operations" );
</pre>
<p> ... and define a title for it. When a user drags the toolbar out of
its location and floats it over the desktop, the toolbar-window will
show "File Operations" as caption.
<p> <pre>        openIcon = TQPixmap( fileopen );
        TQToolButton * fileOpen
            = new <a href="tqtoolbutton.html">TQToolButton</a>( openIcon, "Open File", <a href="tqstring.html#TQString-null">TQString::null</a>,
                               this, TQ_SLOT(choose()), fileTools, "open file" );
</pre>
<p> Now we create the first tool button for the <em>fileTools</em> toolbar
with the appropriate icon and the tool-tip text "Open File".
The <tt>fileopen.xpm</tt> we included at the beginning 
contains the definition of a pixmap called <em>fileopen</em>.
We use this icon to illustrate our first tool button. 
<p> <pre>        saveIcon = TQPixmap( filesave );
        TQToolButton * fileSave
            = new <a href="tqtoolbutton.html">TQToolButton</a>( saveIcon, "Save File", TQString::null,
                               this, TQ_SLOT(save()), fileTools, "save file" );

        printIcon = TQPixmap( fileprint );
        TQToolButton * filePrint
            = new <a href="tqtoolbutton.html">TQToolButton</a>( printIcon, "Print File", TQString::null,
                               this, TQ_SLOT(print()), fileTools, "print file" );
</pre>
<p> In a similar way we create two more tool buttons in this toolbar, each with 
appropriate icons and tool-tip text. All three buttons are connected
to appropriate slots in this object; for example, the "Print File" button 
to <a href="#printer">ApplicationWindow::print()</a>.
<p> <pre>        (void)TQWhatsThis::whatsThisButton( fileTools );
</pre>
<p> The fourth button in the toolbar is somewhat peculiar: it's the one that
provides "What's This?" help. This must be set up using a special
function, as its mouse interface is unusual.
<p> <pre>        const char * fileOpenText = "&lt;p&gt;&lt;img source=\"fileopen\"&gt; "
                     "Click this button to open a &lt;em&gt;new file&lt;/em&gt;.&lt;br&gt;"
                     "You can also select the &lt;b&gt;Open&lt;/b&gt; command "
                     "from the &lt;b&gt;File&lt;/b&gt; menu.&lt;/p&gt;";

        TQWhatsThis::<a href="ntqwhatsthis.html#add">add</a>( fileOpen, fileOpenText );
</pre>
<p> With the above line we add the "What's This?" help-text to the 
<em>fileOpen</em> button...
<p> <pre>        TQMimeSourceFactory::<a href="tqmimesourcefactory.html#defaultFactory">defaultFactory</a>()-&gt;setPixmap( "document-open", openIcon );
</pre>
<p> ... and tell the rich-text engine that when a help-text (like the one
saved in <em>fileOpenText</em>) requests an image named "document-open", the <em>openIcon</em> pixmap is used.
<p> <pre>        const char * fileSaveText = "&lt;p&gt;Click this button to save the file you "
                     "are editing. You will be prompted for a file name.\n"
                     "You can also select the &lt;b&gt;Save&lt;/b&gt; command "
                     "from the &lt;b&gt;File&lt;/b&gt; menu.&lt;/p&gt;";

        TQWhatsThis::<a href="ntqwhatsthis.html#add">add</a>( fileSave, fileSaveText );
        const char * filePrintText = "Click this button to print the file you "
                     "are editing.\n"
                     "You can also select the Print command "
                     "from the File menu.";

        TQWhatsThis::<a href="ntqwhatsthis.html#add">add</a>( filePrint, filePrintText );
</pre>
<p> The "What's This?" help of the remaining two buttons doesn't make use
of pixmaps, therefore all we need to do is to add the help-text to the
button. Be careful though: To invoke the rich-text elements in <tt>fileSaveText()</tt>, the entire string must be surrounded by &lt;p&gt; and
&lt;/p&gt;. In <tt>filePrintText()</tt>, we don't have rich-text elements, so
this is not necessary.
<p> <pre>        <a href="tqpopupmenu.html">TQPopupMenu</a> * file = new <a href="tqpopupmenu.html">TQPopupMenu</a>( this );
        <a href="tqmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;File", file );
</pre>
<p> Next we create a <a href="tqpopupmenu.html">TQPopupMenu</a> for the <em>File</em> menu and 
add it to the menu bar. With the ampersand in front of the letter F,
we allow the user to use the shortcut <em>Alt+F</em> to pop up this menu.
<p> <pre>        file-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( "&amp;New", this, TQ_SLOT(newDoc()), CTRL+Key_N );
</pre>
<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
letter N as marked by the ampersand) or uses the
<em>Ctrl+N</em> accelerator, a new editor-window will pop up. 
<p> <pre>        int id;
        id = file-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( openIcon, "&amp;Open...",
                               this, TQ_SLOT(choose()), CTRL+Key_O );
        file-&gt;<a href="tqmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileOpenText );

        id = file-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( saveIcon, "&amp;Save",
                               this, TQ_SLOT(save()), CTRL+Key_S );
        file-&gt;<a href="tqmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );

        id = file-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( "Save &amp;As...", this, TQ_SLOT(saveAs()) );
        file-&gt;<a href="tqmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );
</pre>
<p> We populate the <em>File</em> menu with three more commands (<em>Open</em>, <em>Save</em> and
<em>Save As</em>), and set "What's This?" help for them. Note in particular
that "What's This?" help and pixmaps are used in both the toolbar (above)
and the menu bar (here). (See <a href="tqaction.html">TQAction</a> and the <tt>examples/action</tt>
example for a shorter and easier approach.)
<p> <pre>        file-&gt;<a href="tqmenudata.html#insertSeparator">insertSeparator</a>();
</pre>
<p> Then we insert a separator, ... 
<p> <pre>        id = file-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( printIcon, "&amp;Print...",
                               this, TQ_SLOT(print()), CTRL+Key_P );
        file-&gt;<a href="tqmenudata.html#setWhatsThis">setWhatsThis</a>( id, filePrintText );

        file-&gt;<a href="tqmenudata.html#insertSeparator">insertSeparator</a>();

        file-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( "&amp;Close", this, TQ_SLOT(<a href="tqwidget.html#close">close</a>()), CTRL+Key_W );
        file-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( "&amp;Quit", tqApp, TQ_SLOT( <a href="ntqapplication.html#closeAllWindows">closeAllWindows</a>() ), CTRL+Key_Q );
</pre>
<p> ... the <em>Print</em> command with "What's This?" help, another separator and
two more commands (<em>Close</em> and <em>Quit</em>) without "What's This?" and pixmaps.
In case of the <em>Close</em> command, the signal is connected 
to the <em>close()</em> slot of the respective <em>ApplicationWindow</em> object whilst
the <em>Quit</em> command affects the entire application.
<p> Because <em>ApplicationWindow</em> is a <a href="tqwidget.html">TQWidget</a>, the <em>close()</em> function
triggers a call to <a href="#closeEvent">closeEvent()</a> which we
will implement later.
<p> <a name="common_constructor"></a>
<pre>        <a href="tqmainwindow.html#menuBar">menuBar</a>()-&gt;insertSeparator();
</pre>
<p> Now that we have done the File menu we shift our focus back to the
menu bar and insert a separator. From now on further menu bar entries
will be aligned to the right if the windows system style requires it.
<p> <pre>        <a href="tqpopupmenu.html">TQPopupMenu</a> * help = new <a href="tqpopupmenu.html">TQPopupMenu</a>( this );
        <a href="tqmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;Help", help );

        help-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( "&amp;About", this, TQ_SLOT(about()), Key_F1 );
        help-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( "About &amp;TQt", this, TQ_SLOT(aboutTQt()) );
        help-&gt;<a href="tqmenudata.html#insertSeparator">insertSeparator</a>();
        help-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( "What's &amp;This", this, TQ_SLOT(<a href="tqmainwindow.html#whatsThis">whatsThis</a>()), SHIFT+Key_F1 );
</pre>
<p> We create a <em>Help</em> menu, add it to the menu bar, and insert a few
commands. Depending on the style it will appear on the right hand 
side of the menu bar or not.
<p> <pre>        e = new <a href="tqtextedit.html">TQTextEdit</a>( this, "editor" );
        e-&gt;<a href="tqwidget.html#setFocus">setFocus</a>();
        <a href="tqmainwindow.html#setCentralWidget">setCentralWidget</a>( e );
</pre>
<p> Now we create a simple text-editor, set the initial focus to it,
and make it the window's central widget.
<p> <a href="tqmainwindow.html#centralWidget">TQMainWindow::centralWidget</a>() is the heart of the entire application:
It's what menu bar, statusbar and toolbars are all arranged around. Since
the central widget is a text editing widget, we can now reveal that
our simple application is a text editor. :)
<p> <pre>        <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Ready", 2000 );
</pre>
<p> We make the statusbar say "Ready" for two seconds at startup, just to
tell the user that the window has finished initialization and can be
used.
<p> <pre>        <a href="tqwidget.html#resize">resize</a>( 450, 600 );
</pre>
<p> Finally it's time to resize the new window to a a nice default size.
<p> <pre>    }
</pre>
<p> We have now finished with the constructor. Now we'll deal with the
destructor. 
<p> <pre>    ApplicationWindow::~ApplicationWindow()
    {
        delete printer;
    }
</pre>
<p> The only thing an <em>ApplicationWindow</em> widget needs to do in its
destructor is  to delete the printer it created. All other objects are
child widgets, which TQt will delete when appropriate.
<p> Now our task is to implement all the slots mentioned in the header file
and used in the constructor.
<p> <a name="newDoc()"></a>
<pre>    void ApplicationWindow::newDoc()
    {
        ApplicationWindow *ed = new ApplicationWindow;
        ed-&gt;<a href="tqwidget.html#setCaption">setCaption</a>("TQt Example - Application");
        ed-&gt;<a href="tqwidget.html#show">show</a>();
    }
</pre>
<p> This slot, connected to the <em>File|New</em> menu item, simply creates a 
new <em>ApplicationWindow</em> and shows it.
<p> <a name="choose()"></a>
<pre>    void ApplicationWindow::choose()
    {
        <a href="tqstring.html">TQString</a> fn = TQFileDialog::<a href="tqfiledialog.html#getOpenFileName">getOpenFileName</a>( TQString::null, TQString::null,
                                                   this);
        if ( !fn.<a href="tqstring.html#isEmpty">isEmpty</a>() )
            load( fn );
        else
            <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Loading aborted", 2000 );
    }
</pre>
<p> The <em>choose()</em> slot is connected to the <em>Open</em> menu item and
tool button. With a little help from <a href="tqfiledialog.html#getOpenFileName">TQFileDialog::getOpenFileName</a>(), it
asks the user for a file name and then either loads that file or gives an
error message in the statusbar.
<p> <pre>    void ApplicationWindow::load( const <a href="tqstring.html">TQString</a> &amp;fileName )
    {
        <a href="tqfile.html">TQFile</a> f( fileName );
        if ( !f.<a href="tqfile.html#open">open</a>( <a href="tqfile.html#open">IO_ReadOnly</a> ) )
            return;

        <a href="tqtextstream.html">TQTextStream</a> ts( &amp;f );
        e-&gt;<a href="tqtextedit.html#setText">setText</a>( ts.<a href="tqtextstream.html#read">read</a>() );
        e-&gt;<a href="tqtextedit.html#setModified">setModified</a>( FALSE );
        <a href="tqwidget.html#setCaption">setCaption</a>( fileName );
        <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Loaded document " + fileName, 2000 );
    }
</pre>
<p> This function loads a file into the editor. When it's done, it sets the
window system caption to the file name and displays a success message in
the statusbar for two seconds. With files that exist but are not
readable, nothing happens.
<p> <a name="save()"></a>
<pre>    void ApplicationWindow::save()
    {
        if ( filename.isEmpty() ) {
            saveAs();
            return;
        }

        <a href="tqstring.html">TQString</a> text = e-&gt;<a href="tqtextedit.html#text">text</a>();
        <a href="tqfile.html">TQFile</a> f( filename );
        if ( !f.<a href="tqfile.html#open">open</a>( <a href="tqfile.html#open">IO_WriteOnly</a> ) ) {
            <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( TQString("Could not write to %1").arg(filename),
                                  2000 );
            return;
        }

        <a href="tqtextstream.html">TQTextStream</a> t( &amp;f );
        t &lt;&lt; text;
        f.<a href="tqfile.html#close">close</a>();
</pre>
<p> As its name suggests, this function saves the current file. If no
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.
Note that there is more than one way to do this:
compare the above <tt>statusBar()-&gt;message()</tt> line with the equivalent
code in the <tt>load()</tt> function.
<p> <pre>        e-&gt;<a href="tqtextedit.html#setModified">setModified</a>( FALSE );
</pre>
<p> Tell the editor that the contents haven't been edited since the last
save. When the user does some further editing and wishes to close the
window without explicit saving, <a href="#closeEvent">ApplicationWindow::closeEvent()</a> will ask about it.
<p> <pre>        <a href="tqwidget.html#setCaption">setCaption</a>( filename );
</pre>
<p> It may be that the document was saved under a different name than the
old caption suggests, so we set the window caption just to be sure.
<p> <pre>        <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( TQString( "File %1 saved" ).arg( filename ), 2000 );
    }
</pre>
<p> With a message in the statusbar, we inform the user that the file
was saved successfully.
<p> <a name="saveAs()"></a>
<pre>    void ApplicationWindow::saveAs()
    {
        <a href="tqstring.html">TQString</a> fn = TQFileDialog::<a href="tqfiledialog.html#getSaveFileName">getSaveFileName</a>( TQString::null, TQString::null,
                                                   this );
        if ( !fn.<a href="tqstring.html#isEmpty">isEmpty</a>() ) {
            filename = fn;
            save();
        } else {
            <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Saving aborted", 2000 );
        }
    }
</pre>
<p> This function asks for a new name, saves the document under that name,
and implicitly changes the window system caption to the new name.
<p> <a name="printer"></a>
<p> <pre>    void ApplicationWindow::print()
    {
        printer-&gt;<a href="ntqprinter.html#setFullPage">setFullPage</a>( TRUE );
        if ( printer-&gt;<a href="ntqprinter.html#setup">setup</a>(this) ) {               // printer dialog
            <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing..." );
            <a href="ntqpainter.html">TQPainter</a> p;
            if( !p.<a href="ntqpainter.html#begin">begin</a>( printer ) ) {               // paint on printer
                <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing aborted", 2000 );
                return;
            }

            <a href="ntqpaintdevicemetrics.html">TQPaintDeviceMetrics</a> metrics( p.<a href="ntqpainter.html#device">device</a>() );
            int dpiy = metrics.<a href="ntqpaintdevicemetrics.html#logicalDpiY">logicalDpiY</a>();
            int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
            <a href="ntqrect.html">TQRect</a> view( margin, margin, metrics.<a href="ntqpaintdevicemetrics.html#width">width</a>() - 2*margin, metrics.<a href="ntqpaintdevicemetrics.html#height">height</a>() - 2*margin );
            <a href="ntqsimplerichtext.html">TQSimpleRichText</a> richText( TQStyleSheet::<a href="tqstylesheet.html#convertFromPlainText">convertFromPlainText</a>(e-&gt;<a href="tqtextedit.html#text">text</a>()),
                                      TQFont(),
                                      e-&gt;<a href="tqtextedit.html#context">context</a>(),
                                      e-&gt;<a href="tqtextedit.html#styleSheet">styleSheet</a>(),
                                      e-&gt;<a href="tqtextedit.html#mimeSourceFactory">mimeSourceFactory</a>(),
                                      view.<a href="ntqrect.html#height">height</a>() );
            richText.<a href="ntqsimplerichtext.html#setWidth">setWidth</a>( &amp;p, view.<a href="ntqrect.html#width">width</a>() );
            int page = 1;
            do {
                richText.<a href="ntqsimplerichtext.html#draw">draw</a>( &amp;p, margin, margin, view, colorGroup() );
                view.<a href="ntqrect.html#moveBy">moveBy</a>( 0, view.<a href="ntqrect.html#height">height</a>() );
                p.<a href="ntqpainter.html#translate">translate</a>( 0 , -view.<a href="ntqrect.html#height">height</a>() );
                p.<a href="ntqpainter.html#drawText">drawText</a>( view.<a href="ntqrect.html#right">right</a>() - p.<a href="ntqpainter.html#fontMetrics">fontMetrics</a>().width( TQString::<a href="tqstring.html#number">number</a>( page ) ),
                            view.<a href="ntqrect.html#bottom">bottom</a>() + p.<a href="ntqpainter.html#fontMetrics">fontMetrics</a>().ascent() + 5, TQString::number( page ) );
                if ( view.<a href="ntqrect.html#top">top</a>() - margin &gt;= richText.<a href="ntqsimplerichtext.html#height">height</a>() )
                    break;
                printer-&gt;<a href="ntqprinter.html#newPage">newPage</a>();
                page++;
            } while (TRUE);

            <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing completed", 2000 );
        } else {
            <a href="tqmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing aborted", 2000 );
        }
    }
</pre>
<p> <em>print()</em> is called by the <em>File|Print</em> menu item and the <em>filePrint</em> 
tool button.
<p> We present the user with the print setup dialog, and abandon printing
if they cancel. 
<p> We create a <a href="ntqsimplerichtext.html">TQSimpleRichText</a> object and give it the text. This object
is able to format the text nicely as one long page. We achieve
pagination by printing one paper page's worth of text from the
TQSimpleRichText page at a time.
<p> Now let's see what happens when a user wishes to <em>close()</em>
an <em>ApplicationWindow</em>.
<p> <a name="closeEvent"></a>
<pre>    void ApplicationWindow::<a href="tqwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">TQCloseEvent</a>* ce )
    {
</pre>
<p> This event gets to process window system close events. A close event is
subtly different from a hide event: hide often means "iconify" whereas
close means that the window is going away for good.
<p> <pre>        if ( !e-&gt;<a href="tqtextedit.html#isModified">isModified</a>() ) {
            ce-&gt;<a href="qcloseevent.html#accept">accept</a>();
            return;
        }
</pre>
<p> If the text hasn't been edited, we just accept the event. The window
will be closed, and because we used the <em>WDestructiveClose</em> <a href="ntqt.html#WidgetFlags">widget flag</a> in the <a href="#ApplicationWindow"><i>ApplicationWindow()</i> constructor</a>, 
the widget will be deleted.
<p> <pre>        switch( TQMessageBox::<a href="ntqmessagebox.html#information">information</a>( this, "TQt Application Example",
                                          "Do you want to save the changes"
                                          " to the document?",
                                          "Yes", "No", "Cancel",
                                          0, 1 ) ) {
</pre>
<p> Otherwise we ask the user: What do you want to do?  
<p> <pre>        case 0:
            save();
            ce-&gt;<a href="qcloseevent.html#accept">accept</a>();
            break;
</pre>
<p> If they want to save and then exit, we do that. 
<p> <pre>        case 1:
            ce-&gt;<a href="qcloseevent.html#accept">accept</a>();
            break;
</pre>
<p> If the user doesn't want to exit, we ignore the close event (there is
a chance that we can't block it but we try).
<p> <pre>        case 2:
        default: // just for sanity
            ce-&gt;<a href="qcloseevent.html#ignore">ignore</a>();
            break;
</pre>
<p> The last case -- the user wants to abandon the edits and exit -- is very
simple.
<p> <pre>        }
    }
</pre>
<p> Last but not least we implement the slots used by the help menu entries.
<p> <pre>    void ApplicationWindow::about()
    {
        TQMessageBox::<a href="ntqmessagebox.html#about">about</a>( this, "TQt Application Example",
                            "This example demonstrates simple use of "
                            "TQMainWindow,\nTQMenuBar and TQToolBar.");
    }

    void ApplicationWindow::aboutTQt()
    {
        TQMessageBox::<a href="ntqmessagebox.html#aboutTQt">aboutTQt</a>( this, "TQt Application Example" );
    }
</pre>
<p> These two slots use ready-made "about" functions to provide some
information about this program and the GUI toolkit it uses. (Although you
don't need to provide an About TQt in your programs, if you use TQt for free
we would appreciate it if you tell people what you're using.)
<p> That was all we needed to write a complete, almost useful application with
nice help-functions, almost as good as the "editors" some computer vendors
ship with their desktops, and in less than 300 lines of code!
<p> <p>See also <a href="step-by-step-examples.html">Step-by-step Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>