source: trunk/doc/html/simple-application-example.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 19.6 KB
Line 
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/application/application.doc:5 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>A Complete Application Window</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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>A Complete Application Window</h1>
33
34
35<p>
36<p> This example program looks like a complete modern application. It
37has a menu bar, it has a tool bar, it has a status bar and works
38like a simple text editor.
39<p> There is a <a href="simple-application.html">walkthrough</a> of
40this example.
41<p> <hr>
42<p> Header file:
43<p> <pre>/****************************************************************************
44** $Id: simple-application-example.html 2051 2007-02-21 10:04:20Z chehrlic $
45**
46** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
47**
48** This file is part of an example program for Qt. This example
49** program may be used, distributed and modified without limitation.
50**
51*****************************************************************************/
52
53#ifndef APPLICATION_H
54#define APPLICATION_H
55
56#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;
57
58class QTextEdit;
59
60class ApplicationWindow: public <a href="qmainwindow.html">QMainWindow</a>
61{
62 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
63
64public:
65 ApplicationWindow();
66 ~ApplicationWindow();
67
68protected:
69 void closeEvent( <a href="qcloseevent.html">QCloseEvent</a>* );
70
71private slots:
72 void newDoc();
73 void choose();
74 void load( const <a href="qstring.html">QString</a> &amp;fileName );
75 void save();
76 void saveAs();
77 void print();
78
79 void about();
80 void aboutQt();
81
82private:
83 <a href="qprinter.html">QPrinter</a> *printer;
84 <a href="qtextedit.html">QTextEdit</a> *e;
85 <a href="qstring.html">QString</a> filename;
86};
87
88
89#endif
90</pre>
91
92<p> <hr>
93<p> Implementation:
94<p> <pre>/****************************************************************************
95** $Id: simple-application-example.html 2051 2007-02-21 10:04:20Z chehrlic $
96**
97** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
98**
99** This file is part of an example program for Qt. This example
100** program may be used, distributed and modified without limitation.
101**
102*****************************************************************************/
103
104#include "application.h"
105
106#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
107#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
108#include &lt;<a href="qtoolbar-h.html">qtoolbar.h</a>&gt;
109#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;
110#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
111#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
112#include &lt;<a href="qtextedit-h.html">qtextedit.h</a>&gt;
113#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
114#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
115#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
116#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
117#include &lt;<a href="qprinter-h.html">qprinter.h</a>&gt;
118#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
119#include &lt;<a href="qaccel-h.html">qaccel.h</a>&gt;
120#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
121#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
122#include &lt;<a href="qpaintdevicemetrics-h.html">qpaintdevicemetrics.h</a>&gt;
123#include &lt;<a href="qwhatsthis-h.html">qwhatsthis.h</a>&gt;
124#include &lt;<a href="qsimplerichtext-h.html">qsimplerichtext.h</a>&gt;
125
126#include "filesave.xpm"
127#include "fileopen.xpm"
128#include "fileprint.xpm"
129
130<a name="f459"></a>ApplicationWindow::ApplicationWindow()
131 : <a href="qmainwindow.html">QMainWindow</a>( 0, "example application main window", WDestructiveClose | WGroupLeader )
132{
133 printer = new <a href="qprinter.html">QPrinter</a>( QPrinter::HighResolution );
134 <a href="qpixmap.html">QPixmap</a> openIcon, saveIcon, printIcon;
135
136 <a href="qtoolbar.html">QToolBar</a> * fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );
137<a name="x1590"></a> fileTools-&gt;<a href="qtoolbar.html#setLabel">setLabel</a>( "File Operations" );
138
139 openIcon = QPixmap( fileopen );
140 QToolButton * fileOpen
141 = new <a href="qtoolbutton.html">QToolButton</a>( openIcon, "Open File", <a href="qstring.html#QString-null">QString::null</a>,
142 this, SLOT(choose()), fileTools, "open file" );
143
144 saveIcon = QPixmap( filesave );
145 QToolButton * fileSave
146 = new <a href="qtoolbutton.html">QToolButton</a>( saveIcon, "Save File", QString::null,
147 this, SLOT(save()), fileTools, "save file" );
148
149 printIcon = QPixmap( fileprint );
150 QToolButton * filePrint
151 = new <a href="qtoolbutton.html">QToolButton</a>( printIcon, "Print File", QString::null,
152 this, SLOT(print()), fileTools, "print file" );
153
154
155 (void)QWhatsThis::whatsThisButton( fileTools );
156
157 const char * fileOpenText = "&lt;p&gt;&lt;img source=\"fileopen\"&gt; "
158 "Click this button to open a &lt;em&gt;new file&lt;/em&gt;.&lt;br&gt;"
159 "You can also select the &lt;b&gt;Open&lt;/b&gt; command "
160 "from the &lt;b&gt;File&lt;/b&gt; menu.&lt;/p&gt;";
161
162<a name="x1591"></a> QWhatsThis::<a href="qwhatsthis.html#add">add</a>( fileOpen, fileOpenText );
163
164<a name="x1558"></a> QMimeSourceFactory::<a href="qmimesourcefactory.html#defaultFactory">defaultFactory</a>()-&gt;setPixmap( "fileopen", openIcon );
165
166 const char * fileSaveText = "&lt;p&gt;Click this button to save the file you "
167 "are editing. You will be prompted for a file name.\n"
168 "You can also select the &lt;b&gt;Save&lt;/b&gt; command "
169 "from the &lt;b&gt;File&lt;/b&gt; menu.&lt;/p&gt;";
170
171 QWhatsThis::<a href="qwhatsthis.html#add">add</a>( fileSave, fileSaveText );
172
173 const char * filePrintText = "Click this button to print the file you "
174 "are editing.\n"
175 "You can also select the Print command "
176 "from the File menu.";
177
178 QWhatsThis::<a href="qwhatsthis.html#add">add</a>( filePrint, filePrintText );
179
180
181 <a href="qpopupmenu.html">QPopupMenu</a> * file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
182 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;File", file );
183
184
185 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;New", this, SLOT(newDoc()), CTRL+Key_N );
186
187 int id;
188 id = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( openIcon, "&amp;Open...",
189 this, SLOT(choose()), CTRL+Key_O );
190<a name="x1554"></a> file-&gt;<a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileOpenText );
191
192 id = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( saveIcon, "&amp;Save",
193 this, SLOT(save()), CTRL+Key_S );
194 file-&gt;<a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );
195
196 id = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Save &amp;As...", this, SLOT(saveAs()) );
197 file-&gt;<a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );
198
199 file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
200
201 id = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( printIcon, "&amp;Print...",
202 this, SLOT(print()), CTRL+Key_P );
203 file-&gt;<a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, filePrintText );
204
205 file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
206
207 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Close", this, SLOT(<a href="qwidget.html#close">close</a>()), CTRL+Key_W );
208
209<a name="x1544"></a> file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Quit", qApp, SLOT( <a href="qapplication.html#closeAllWindows">closeAllWindows</a>() ), CTRL+Key_Q );
210
211 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertSeparator();
212
213 <a href="qpopupmenu.html">QPopupMenu</a> * help = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
214 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;Help", help );
215
216 help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;About", this, SLOT(about()), Key_F1 );
217 help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "About &amp;Qt", this, SLOT(aboutQt()) );
218 help-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
219 help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "What's &amp;This", this, SLOT(<a href="qmainwindow.html#whatsThis">whatsThis</a>()), SHIFT+Key_F1 );
220
221 e = new <a href="qtextedit.html">QTextEdit</a>( this, "editor" );
222 e-&gt;<a href="qwidget.html#setFocus">setFocus</a>();
223 <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( e );
224 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Ready", 2000 );
225
226 <a href="qwidget.html#resize">resize</a>( 450, 600 );
227}
228
229
230ApplicationWindow::~ApplicationWindow()
231{
232 delete printer;
233}
234
235
236
237void <a name="f460"></a>ApplicationWindow::newDoc()
238{
239 ApplicationWindow *ed = new ApplicationWindow;
240 ed-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Application");
241 ed-&gt;<a href="qwidget.html#show">show</a>();
242}
243
244void <a name="f461"></a>ApplicationWindow::choose()
245{
246<a name="x1549"></a> <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, QString::null,
247 this);
248 if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() )
249 load( fn );
250 else
251 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Loading aborted", 2000 );
252}
253
254
255void <a name="f462"></a>ApplicationWindow::load( const <a href="qstring.html">QString</a> &amp;fileName )
256{
257 <a href="qfile.html">QFile</a> f( fileName );
258<a name="x1548"></a> if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) )
259 return;
260
261 <a href="qtextstream.html">QTextStream</a> ts( &amp;f );
262<a name="x1589"></a><a name="x1586"></a> e-&gt;<a href="qtextedit.html#setText">setText</a>( ts.<a href="qtextstream.html#read">read</a>() );
263<a name="x1585"></a> e-&gt;<a href="qtextedit.html#setModified">setModified</a>( FALSE );
264 <a href="qwidget.html#setCaption">setCaption</a>( fileName );
265 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Loaded document " + fileName, 2000 );
266}
267
268
269void <a name="f463"></a>ApplicationWindow::save()
270{
271 if ( filename.isEmpty() ) {
272 saveAs();
273 return;
274 }
275
276<a name="x1588"></a> <a href="qstring.html">QString</a> text = e-&gt;<a href="qtextedit.html#text">text</a>();
277 <a href="qfile.html">QFile</a> f( filename );
278 if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) ) {
279 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( QString("Could not write to %1").arg(filename),
280 2000 );
281 return;
282 }
283
284 <a href="qtextstream.html">QTextStream</a> t( &amp;f );
285 t &lt;&lt; text;
286 f.<a href="qfile.html#close">close</a>();
287
288 e-&gt;<a href="qtextedit.html#setModified">setModified</a>( FALSE );
289
290 <a href="qwidget.html#setCaption">setCaption</a>( filename );
291
292 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( QString( "File %1 saved" ).arg( filename ), 2000 );
293}
294
295
296void <a name="f464"></a>ApplicationWindow::saveAs()
297{
298<a name="x1550"></a> <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>( QString::null, QString::null,
299 this );
300 if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
301 filename = fn;
302 save();
303 } else {
304 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Saving aborted", 2000 );
305 }
306}
307
308
309void <a name="f465"></a>ApplicationWindow::print()
310{
311<a name="x1568"></a> printer-&gt;<a href="qprinter.html#setFullPage">setFullPage</a>( TRUE );
312<a name="x1569"></a> if ( printer-&gt;<a href="qprinter.html#setup">setup</a>(this) ) { // printer dialog
313 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing..." );
314 <a href="qpainter.html">QPainter</a> p;
315 if( !p.<a href="qpainter.html#begin">begin</a>( printer ) ) { // paint on printer
316 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing aborted", 2000 );
317 return;
318 }
319
320<a name="x1563"></a> <a href="qpaintdevicemetrics.html">QPaintDeviceMetrics</a> metrics( p.<a href="qpainter.html#device">device</a>() );
321<a name="x1560"></a> int dpiy = metrics.<a href="qpaintdevicemetrics.html#logicalDpiY">logicalDpiY</a>();
322 int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
323<a name="x1561"></a><a name="x1559"></a> <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 );
324<a name="x1581"></a> <a href="qsimplerichtext.html">QSimpleRichText</a> richText( QStyleSheet::<a href="qstylesheet.html#convertFromPlainText">convertFromPlainText</a>(e-&gt;<a href="qtextedit.html#text">text</a>()),
325 QFont(),
326<a name="x1582"></a> e-&gt;<a href="qtextedit.html#context">context</a>(),
327<a name="x1587"></a> e-&gt;<a href="qtextedit.html#styleSheet">styleSheet</a>(),
328<a name="x1584"></a> e-&gt;<a href="qtextedit.html#mimeSourceFactory">mimeSourceFactory</a>(),
329 view.<a href="qrect.html#height">height</a>() );
330<a name="x1578"></a> richText.<a href="qsimplerichtext.html#setWidth">setWidth</a>( &amp;p, view.<a href="qrect.html#width">width</a>() );
331 int page = 1;
332 do {
333<a name="x1576"></a> richText.<a href="qsimplerichtext.html#draw">draw</a>( &amp;p, margin, margin, view, colorGroup() );
334<a name="x1572"></a> view.<a href="qrect.html#moveBy">moveBy</a>( 0, view.<a href="qrect.html#height">height</a>() );
335<a name="x1566"></a> p.<a href="qpainter.html#translate">translate</a>( 0 , -view.<a href="qrect.html#height">height</a>() );
336<a name="x1580"></a><a name="x1573"></a><a name="x1565"></a> 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 ) ),
337<a name="x1570"></a> view.<a href="qrect.html#bottom">bottom</a>() + p.<a href="qpainter.html#fontMetrics">fontMetrics</a>().ascent() + 5, QString::number( page ) );
338<a name="x1577"></a> if ( view.<a href="qrect.html#top">top</a>() - margin &gt;= richText.<a href="qsimplerichtext.html#height">height</a>() )
339 break;
340<a name="x1567"></a> printer-&gt;<a href="qprinter.html#newPage">newPage</a>();
341 page++;
342 } while (TRUE);
343
344 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing completed", 2000 );
345 } else {
346 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing aborted", 2000 );
347 }
348}
349
350<a name="x1592"></a>void ApplicationWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a>* ce )
351{
352<a name="x1583"></a> if ( !e-&gt;<a href="qtextedit.html#isModified">isModified</a>() ) {
353<a name="x1545"></a> ce-&gt;<a href="qcloseevent.html#accept">accept</a>();
354 return;
355 }
356
357<a name="x1557"></a> switch( QMessageBox::<a href="qmessagebox.html#information">information</a>( this, "Qt Application Example",
358 "Do you want to save the changes"
359 " to the document?",
360 "Yes", "No", "Cancel",
361 0, 1 ) ) {
362 case 0:
363 save();
364 ce-&gt;<a href="qcloseevent.html#accept">accept</a>();
365 break;
366 case 1:
367 ce-&gt;<a href="qcloseevent.html#accept">accept</a>();
368 break;
369 case 2:
370 default: // just for sanity
371<a name="x1546"></a> ce-&gt;<a href="qcloseevent.html#ignore">ignore</a>();
372 break;
373 }
374}
375
376
377void <a name="f466"></a>ApplicationWindow::about()
378{
379<a name="x1555"></a> QMessageBox::<a href="qmessagebox.html#about">about</a>( this, "Qt Application Example",
380 "This example demonstrates simple use of "
381 "QMainWindow,\nQMenuBar and QToolBar.");
382}
383
384
385void <a name="f467"></a>ApplicationWindow::aboutQt()
386{
387<a name="x1556"></a> QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Qt Application Example" );
388}
389</pre>
390
391<p> <hr>
392<p> Main:
393<p> <pre>/****************************************************************************
394** $Id: simple-application-example.html 2051 2007-02-21 10:04:20Z chehrlic $
395**
396** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
397**
398** This file is part of an example program for Qt. This example
399** program may be used, distributed and modified without limitation.
400**
401*****************************************************************************/
402
403#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
404#include "application.h"
405
406int main( int argc, char ** argv ) {
407 <a href="qapplication.html">QApplication</a> a( argc, argv );
408 ApplicationWindow *mw = new ApplicationWindow();
409 mw-&gt;<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Application" );
410<a name="x1598"></a> mw-&gt;<a href="qwidget.html#show">show</a>();
411<a name="x1599"></a> a.<a href="qobject.html#connect">connect</a>( &amp;a, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), &amp;a, SLOT(<a href="qapplication.html#quit">quit</a>()) );
412 return a.<a href="qapplication.html#exec">exec</a>();
413}
414</pre>
415
416<p>See also <a href="examples.html">Examples</a>.
417
418<!-- eof -->
419<p><address><hr><div align=center>
420<table width=100% cellspacing=0 border=0><tr>
421<td>Copyright &copy; 2007
422<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
423<td align=right><div align=right>Qt 3.3.8</div>
424</table></div></address></body>
425</html>
Note: See TracBrowser for help on using the repository browser.