source: trunk/doc/html/mdi-example.html

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

reference documentation added

File size: 27.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/mdi/mdi.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>An MDI Application</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>An MDI Application</h1>
33
34
35<p>
36This example program is just like the application example, except
37that it provides a Multiple Document Interface (MDI).
38<p> <hr>
39<p> Header file:
40<p> <pre>/****************************************************************************
41** $Id: mdi-example.html 2051 2007-02-21 10:04:20Z chehrlic $
42**
43** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
44**
45** This file is part of an example program for Qt. This example
46** program may be used, distributed and modified without limitation.
47**
48*****************************************************************************/
49
50#ifndef APPLICATION_H
51#define APPLICATION_H
52
53#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;
54#include &lt;<a href="qptrlist-h.html">qptrlist.h</a>&gt;
55
56class QTextEdit;
57class QToolBar;
58class QPopupMenu;
59class QWorkspace;
60class QPopupMenu;
61class QMovie;
62
63class MDIWindow: public <a href="qmainwindow.html">QMainWindow</a>
64{
65 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
66public:
67 MDIWindow( <a href="qwidget.html">QWidget</a>* parent, const char* name, int wflags );
68 ~MDIWindow();
69
70 void load( const <a href="qstring.html">QString</a>&amp; fn );
71 void save();
72 void saveAs();
73 void print( <a href="qprinter.html">QPrinter</a>* );
74
75protected:
76 void closeEvent( <a href="qcloseevent.html">QCloseEvent</a> * );
77
78signals:
79 void message(const <a href="qstring.html">QString</a>&amp;, int );
80
81private:
82 <a href="qtextedit.html">QTextEdit</a>* medit;
83 <a href="qmovie.html">QMovie</a> * mmovie;
84 <a href="qstring.html">QString</a> filename;
85};
86
87
88class ApplicationWindow: public <a href="qmainwindow.html">QMainWindow</a>
89{
90 Q_OBJECT
91public:
92 ApplicationWindow();
93 ~ApplicationWindow();
94
95protected:
96 void closeEvent( <a href="qcloseevent.html">QCloseEvent</a> * );
97
98private slots:
99 MDIWindow* newDoc();
100 void load();
101 void save();
102 void saveAs();
103 void print();
104 void closeWindow();
105 void tileHorizontal();
106
107 void about();
108 void aboutQt();
109
110 void windowsMenuAboutToShow();
111 void windowsMenuActivated( int id );
112
113private:
114 <a href="qprinter.html">QPrinter</a> *printer;
115 <a href="qworkspace.html">QWorkspace</a>* ws;
116 <a href="qtoolbar.html">QToolBar</a> *fileTools;
117 <a href="qpopupmenu.html">QPopupMenu</a>* windowsMenu;
118};
119
120
121#endif
122</pre>
123
124<p> <hr>
125<p> Implementation:
126<p> <pre>/****************************************************************************
127** $Id: mdi-example.html 2051 2007-02-21 10:04:20Z chehrlic $
128**
129** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
130**
131** This file is part of an example program for Qt. This example
132** program may be used, distributed and modified without limitation.
133**
134*****************************************************************************/
135
136#include "application.h"
137#include &lt;<a href="qworkspace-h.html">qworkspace.h</a>&gt;
138#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
139#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
140#include &lt;<a href="qtoolbar-h.html">qtoolbar.h</a>&gt;
141#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;
142#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
143#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
144#include &lt;<a href="qmovie-h.html">qmovie.h</a>&gt;
145#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
146#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
147#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
148#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
149#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
150#include &lt;<a href="qprinter-h.html">qprinter.h</a>&gt;
151#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
152#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
153#include &lt;<a href="qaccel-h.html">qaccel.h</a>&gt;
154#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
155#include &lt;<a href="qtextedit-h.html">qtextedit.h</a>&gt;
156#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
157#include &lt;<a href="qpaintdevicemetrics-h.html">qpaintdevicemetrics.h</a>&gt;
158#include &lt;<a href="qwhatsthis-h.html">qwhatsthis.h</a>&gt;
159#include &lt;<a href="qobjectlist-h.html">qobjectlist.h</a>&gt;
160#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
161#include &lt;<a href="qsimplerichtext-h.html">qsimplerichtext.h</a>&gt;
162
163#include "filesave.xpm"
164#include "fileopen.xpm"
165#include "fileprint.xpm"
166
167
168const char * fileOpenText = "Click this button to open a &lt;em&gt;new file&lt;/em&gt;. &lt;br&gt;&lt;br&gt;"
169"You can also select the &lt;b&gt;Open command&lt;/b&gt; from the File menu.";
170const char * fileSaveText = "Click this button to save the file you are "
171"editing. You will be prompted for a file name.\n\n"
172"You can also select the Save command from the File menu.\n\n"
173"Note that implementing this function is left as an exercise for the reader.";
174const char * filePrintText = "Click this button to print the file you "
175"are editing.\n\n"
176"You can also select the Print command from the File menu.";
177
178<a name="f535"></a>ApplicationWindow::ApplicationWindow()
179 : <a href="qmainwindow.html">QMainWindow</a>( 0, "example application main window", WDestructiveClose )
180{
181 int id;
182
183 <a href="qpixmap.html">QPixmap</a> openIcon, saveIcon;
184
185 fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );
186 <a href="qmainwindow.html#addToolBar">addToolBar</a>( fileTools, tr( "File Operations" ), DockTop, TRUE );
187
188 openIcon = QPixmap( fileopen );
189 QToolButton * fileOpen
190 = new <a href="qtoolbutton.html">QToolButton</a>( openIcon, "Open File", <a href="qstring.html#QString-null">QString::null</a>,
191 this, SLOT(load()), fileTools, "open file" );
192
193 saveIcon = QPixmap( filesave );
194 QToolButton * fileSave
195 = new <a href="qtoolbutton.html">QToolButton</a>( saveIcon, "Save File", QString::null,
196 this, SLOT(save()), fileTools, "save file" );
197
198#ifndef QT_NO_PRINTER
199 printer = new <a href="qprinter.html">QPrinter</a>( QPrinter::HighResolution );
200 <a href="qpixmap.html">QPixmap</a> printIcon;
201
202 printIcon = QPixmap( fileprint );
203 QToolButton * filePrint
204 = new <a href="qtoolbutton.html">QToolButton</a>( printIcon, "Print File", QString::null,
205 this, SLOT(print()), fileTools, "print file" );
206<a name="x2075"></a> QWhatsThis::<a href="qwhatsthis.html#add">add</a>( filePrint, filePrintText );
207#endif
208
209 (void)QWhatsThis::whatsThisButton( fileTools );
210
211 QWhatsThis::<a href="qwhatsthis.html#add">add</a>( fileOpen, fileOpenText );
212 QWhatsThis::<a href="qwhatsthis.html#add">add</a>( fileSave, fileSaveText );
213
214 <a href="qpopupmenu.html">QPopupMenu</a> * file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
215 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;File", file );
216
217 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;New", this, SLOT(newDoc()), CTRL+Key_N );
218
219 id = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( openIcon, "&amp;Open...",
220 this, SLOT(load()), CTRL+Key_O );
221<a name="x2035"></a> file-&gt;<a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileOpenText );
222
223 id = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( saveIcon, "&amp;Save",
224 this, SLOT(save()), CTRL+Key_S );
225 file-&gt;<a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );
226 id = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Save &amp;As...", this, SLOT(saveAs()) );
227 file-&gt;<a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );
228#ifndef QT_NO_PRINTER
229 file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
230 id = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( printIcon, "&amp;Print...",
231 this, SLOT(print()), CTRL+Key_P );
232 file-&gt;<a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, filePrintText );
233#endif
234 file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
235 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Close", this, SLOT(closeWindow()), CTRL+Key_W );
236<a name="x2020"></a> file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Quit", qApp, SLOT( <a href="qapplication.html#closeAllWindows">closeAllWindows</a>() ), CTRL+Key_Q );
237
238 windowsMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
239<a name="x2049"></a> windowsMenu-&gt;<a href="qpopupmenu.html#setCheckable">setCheckable</a>( TRUE );
240<a name="x2048"></a> <a href="qobject.html#connect">connect</a>( windowsMenu, SIGNAL( <a href="qpopupmenu.html#aboutToShow">aboutToShow</a>() ),
241 this, SLOT( windowsMenuAboutToShow() ) );
242 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;Windows", windowsMenu );
243
244 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertSeparator();
245 <a href="qpopupmenu.html">QPopupMenu</a> * help = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
246 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;Help", help );
247
248 help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;About", this, SLOT(about()), Key_F1);
249 help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "About &amp;Qt", this, SLOT(aboutQt()));
250 help-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
251 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);
252
253 <a href="qvbox.html">QVBox</a>* vb = new <a href="qvbox.html">QVBox</a>( this );
254 vb-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::StyledPanel | QFrame::Sunken );
255 ws = new <a href="qworkspace.html">QWorkspace</a>( vb );
256<a name="x2092"></a> ws-&gt;<a href="qworkspace.html#setScrollBarsEnabled">setScrollBarsEnabled</a>( TRUE );
257 <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( vb );
258
259 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Ready", 2000 );
260}
261
262
263ApplicationWindow::~ApplicationWindow()
264{
265#ifndef QT_NO_PRINTER
266 delete printer;
267#endif
268}
269
270
271
272MDIWindow* <a name="f536"></a>ApplicationWindow::newDoc()
273{
274 MDIWindow* w = new MDIWindow( ws, 0, WDestructiveClose );
275 <a href="qobject.html#connect">connect</a>( w, SIGNAL( message(const <a href="qstring.html">QString</a>&amp;, int) ), statusBar(), SLOT( message(const <a href="qstring.html">QString</a>&amp;, int )) );
276 w-&gt;<a href="qwidget.html#setCaption">setCaption</a>("unnamed document");
277<a name="x2085"></a> w-&gt;<a href="qwidget.html#setIcon">setIcon</a>( QPixmap("document.xpm") );
278 // show the very first window in maximized mode
279<a name="x2094"></a> if ( ws-&gt;<a href="qworkspace.html#windowList">windowList</a>().isEmpty() )
280<a name="x2087"></a> w-&gt;<a href="qwidget.html#showMaximized">showMaximized</a>();
281 else
282 w-&gt;<a href="qwidget.html#show">show</a>();
283 return w;
284}
285
286void <a name="f537"></a>ApplicationWindow::load()
287{
288 <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, QString::null, this );
289 if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
290 MDIWindow* w = newDoc();
291 w-&gt;load( fn );
292 } else {
293 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Loading aborted", 2000 );
294 }
295}
296
297void <a name="f538"></a>ApplicationWindow::save()
298{
299<a name="x2090"></a> MDIWindow* m = (MDIWindow*)ws-&gt;<a href="qworkspace.html#activeWindow">activeWindow</a>();
300 if ( m )
301 m-&gt;save();
302}
303
304
305void <a name="f539"></a>ApplicationWindow::saveAs()
306{
307 MDIWindow* m = (MDIWindow*)ws-&gt;<a href="qworkspace.html#activeWindow">activeWindow</a>();
308 if ( m )
309 m-&gt;saveAs();
310}
311
312
313void <a name="f540"></a>ApplicationWindow::print()
314{
315#ifndef QT_NO_PRINTER
316 MDIWindow* m = (MDIWindow*)ws-&gt;<a href="qworkspace.html#activeWindow">activeWindow</a>();
317 if ( m )
318 m-&gt;print( printer );
319#endif
320}
321
322
323void <a name="f541"></a>ApplicationWindow::closeWindow()
324{
325 MDIWindow* m = (MDIWindow*)ws-&gt;<a href="qworkspace.html#activeWindow">activeWindow</a>();
326 if ( m )
327<a name="x2076"></a> m-&gt;<a href="qwidget.html#close">close</a>();
328}
329
330void <a name="f542"></a>ApplicationWindow::about()
331{
332<a name="x2036"></a> QMessageBox::<a href="qmessagebox.html#about">about</a>( this, "Qt Application Example",
333 "This example demonstrates simple use of\n "
334 "Qt's Multiple Document Interface (MDI).");
335}
336
337
338void <a name="f543"></a>ApplicationWindow::aboutQt()
339{
340 QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Qt Application Example" );
341}
342
343
344void <a name="f544"></a>ApplicationWindow::windowsMenuAboutToShow()
345{
346<a name="x2029"></a> windowsMenu-&gt;<a href="qmenudata.html#clear">clear</a>();
347<a name="x2091"></a> int cascadeId = windowsMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Cascade", ws, SLOT(<a href="qworkspace.html#cascade">cascade</a>() ) );
348<a name="x2093"></a> int tileId = windowsMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Tile", ws, SLOT(<a href="qworkspace.html#tile">tile</a>() ) );
349 int horTileId = windowsMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Tile &amp;Horizontally", this, SLOT(tileHorizontal() ) );
350 if ( ws-&gt;<a href="qworkspace.html#windowList">windowList</a>().isEmpty() ) {
351<a name="x2033"></a> windowsMenu-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( cascadeId, FALSE );
352 windowsMenu-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( tileId, FALSE );
353 windowsMenu-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( horTileId, FALSE );
354 }
355 windowsMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
356 QWidgetList windows = ws-&gt;<a href="qworkspace.html#windowList">windowList</a>();
357<a name="x2054"></a> for ( int i = 0; i &lt; int(windows.count()); ++i ) {
358<a name="x2053"></a> int id = windowsMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>(windows.at(i)-&gt;caption(),
359 this, SLOT( windowsMenuActivated( int ) ) );
360<a name="x2034"></a> windowsMenu-&gt;<a href="qmenudata.html#setItemParameter">setItemParameter</a>( id, i );
361<a name="x2032"></a> windowsMenu-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( id, ws-&gt;<a href="qworkspace.html#activeWindow">activeWindow</a>() == windows.at(i) );
362 }
363}
364
365void <a name="f545"></a>ApplicationWindow::windowsMenuActivated( int id )
366{
367 <a href="qwidget.html">QWidget</a>* w = ws-&gt;<a href="qworkspace.html#windowList">windowList</a>().at( id );
368 if ( w )
369<a name="x2088"></a> w-&gt;<a href="qwidget.html#showNormal">showNormal</a>();
370<a name="x2084"></a> w-&gt;<a href="qwidget.html#setFocus">setFocus</a>();
371}
372
373void <a name="f546"></a>ApplicationWindow::tileHorizontal()
374{
375 // primitive horizontal tiling
376 QWidgetList windows = ws-&gt;<a href="qworkspace.html#windowList">windowList</a>();
377 if ( !windows.count() )
378 return;
379
380 if (ws-&gt;<a href="qworkspace.html#activeWindow">activeWindow</a>())
381 ws-&gt;<a href="qworkspace.html#activeWindow">activeWindow</a>()-&gt;showNormal();
382
383<a name="x2078"></a> int heightForEach = ws-&gt;<a href="qwidget.html#height">height</a>() / windows.count();
384 int y = 0;
385 for ( int i = 0; i &lt; int(windows.count()); ++i ) {
386 <a href="qwidget.html">QWidget</a> *window = windows.at(i);
387<a name="x2081"></a><a name="x2080"></a> int preferredHeight = window-&gt;<a href="qwidget.html#minimumHeight">minimumHeight</a>()+window-&gt;<a href="qwidget.html#parentWidget">parentWidget</a>()-&gt;baseSize().height();
388 int actHeight = QMAX(heightForEach, preferredHeight);
389
390<a name="x2089"></a> window-&gt;<a href="qwidget.html#parentWidget">parentWidget</a>()-&gt;setGeometry( 0, y, ws-&gt;<a href="qwidget.html#width">width</a>(), actHeight );
391 y += actHeight;
392 }
393}
394
395<a name="x2077"></a>void ApplicationWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a> *e )
396{
397 QWidgetList windows = ws-&gt;<a href="qworkspace.html#windowList">windowList</a>();
398 if ( windows.count() ) {
399 for ( int i = 0; i &lt; int(windows.count()); ++i ) {
400 <a href="qwidget.html">QWidget</a> *window = windows.at( i );
401 if ( !window-&gt;<a href="qwidget.html#close">close</a>() ) {
402<a name="x2022"></a> e-&gt;<a href="qcloseevent.html#ignore">ignore</a>();
403 return;
404 }
405 }
406 }
407
408 QMainWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( e );
409}
410
411<a name="f547"></a>MDIWindow::MDIWindow( <a href="qwidget.html">QWidget</a>* parent, const char* name, int wflags )
412 : <a href="qmainwindow.html">QMainWindow</a>( parent, name, wflags )
413{
414 mmovie = 0;
415 medit = new <a href="qtextedit.html">QTextEdit</a>( this );
416 <a href="qwidget.html#setFocusProxy">setFocusProxy</a>( medit );
417 <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( medit );
418}
419
420MDIWindow::~MDIWindow()
421{
422 delete mmovie;
423}
424
425void MDIWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a> *e )
426{
427<a name="x2069"></a> if ( medit-&gt;<a href="qtextedit.html#isModified">isModified</a>() ) {
428 switch( QMessageBox::<a href="qmessagebox.html#warning">warning</a>( this, "Save Changes",
429 <a href="qobject.html#tr">tr</a>("Save changes to %1?").arg( <a href="qwidget.html#caption">caption</a>() ),
430 <a href="qobject.html#tr">tr</a>("Yes"), tr("No"), tr("Cancel") ) ) {
431 case 0:
432 {
433 save();
434 if ( !filename.isEmpty() )
435<a name="x2021"></a> e-&gt;<a href="qcloseevent.html#accept">accept</a>();
436 else
437 e-&gt;<a href="qcloseevent.html#ignore">ignore</a>();
438 }
439 break;
440 case 1:
441 e-&gt;<a href="qcloseevent.html#accept">accept</a>();
442 break;
443 default:
444 e-&gt;<a href="qcloseevent.html#ignore">ignore</a>();
445 break;
446 }
447 } else {
448 e-&gt;<a href="qcloseevent.html#accept">accept</a>();
449 }
450}
451
452void <a name="f548"></a>MDIWindow::load( const <a href="qstring.html">QString</a>&amp; fn )
453{
454 filename = fn;
455 <a href="qfile.html">QFile</a> f( filename );
456 if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) )
457 return;
458
459<a name="x2064"></a> if(fn.<a href="qstring.html#contains">contains</a>(".gif")) {
460 <a href="qwidget.html">QWidget</a> * tmp=new <a href="qwidget.html">QWidget</a>(this);
461 <a href="qwidget.html#setFocusProxy">setFocusProxy</a>(tmp);
462 <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>(tmp);
463<a name="x2079"></a> medit-&gt;<a href="qwidget.html#hide">hide</a>();
464 delete medit;
465 <a href="qmovie.html">QMovie</a> * qm=new <a href="qmovie.html">QMovie</a>(fn);
466#ifdef Q_WS_QWS // temporary speed-test hack
467<a name="x2039"></a> qm-&gt;setDisplayWidget(tmp);
468#endif
469<a name="x2082"></a> tmp-&gt;<a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>(QWidget::NoBackground);
470 tmp-&gt;<a href="qwidget.html#show">show</a>();
471 mmovie=qm;
472 } else {
473 mmovie = 0;
474
475 <a href="qtextstream.html">QTextStream</a> t(&amp;f);
476<a name="x2074"></a> <a href="qstring.html">QString</a> s = t.<a href="qtextstream.html#read">read</a>();
477 medit-&gt;<a href="qtextedit.html#setText">setText</a>( s );
478<a name="x2023"></a> f.<a href="qfile.html#close">close</a>();
479
480
481 }
482 <a href="qwidget.html#setCaption">setCaption</a>( filename );
483 emit message( QString("Loaded document %1").arg(filename), 2000 );
484}
485
486void <a name="f549"></a>MDIWindow::save()
487{
488 if ( filename.isEmpty() ) {
489 saveAs();
490 return;
491 }
492
493<a name="x2073"></a> <a href="qstring.html">QString</a> text = medit-&gt;<a href="qtextedit.html#text">text</a>();
494 <a href="qfile.html">QFile</a> f( filename );
495 if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) ) {
496 emit message( QString("Could not write to %1").arg(filename),
497 2000 );
498 return;
499 }
500
501 <a href="qtextstream.html">QTextStream</a> t( &amp;f );
502 t &lt;&lt; text;
503 f.<a href="qfile.html#close">close</a>();
504
505 <a href="qwidget.html#setCaption">setCaption</a>( filename );
506
507 emit message( QString( "File %1 saved" ).arg( filename ), 2000 );
508}
509
510void <a name="f550"></a>MDIWindow::saveAs()
511{
512 <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>( filename, QString::null, this );
513 if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
514 filename = fn;
515 save();
516 } else {
517 emit message( "Saving aborted", 2000 );
518 }
519}
520
521void <a name="f551"></a>MDIWindow::print( <a href="qprinter.html">QPrinter</a>* printer)
522{
523#ifndef QT_NO_PRINTER
524 int pageNo = 1;
525
526<a name="x2052"></a> if ( printer-&gt;<a href="qprinter.html#setup">setup</a>(this) ) { // printer dialog
527<a name="x2051"></a> printer-&gt;<a href="qprinter.html#setFullPage">setFullPage</a>( TRUE );
528 emit message( "Printing...", 0 );
529 <a href="qpainter.html">QPainter</a> p;
530 if ( !p.<a href="qpainter.html#begin">begin</a>( printer ) )
531 return; // paint on printer
532<a name="x2044"></a> <a href="qpaintdevicemetrics.html">QPaintDeviceMetrics</a> metrics( p.<a href="qpainter.html#device">device</a>() );
533<a name="x2041"></a> int dpiy = metrics.<a href="qpaintdevicemetrics.html#logicalDpiY">logicalDpiY</a>();
534 int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
535<a name="x2042"></a><a name="x2040"></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 );
536<a name="x2067"></a> <a href="qsimplerichtext.html">QSimpleRichText</a> richText( QStyleSheet::<a href="qstylesheet.html#convertFromPlainText">convertFromPlainText</a>(medit-&gt;<a href="qtextedit.html#text">text</a>()),
537 QFont(),
538<a name="x2068"></a> medit-&gt;<a href="qtextedit.html#context">context</a>(),
539<a name="x2072"></a> medit-&gt;<a href="qtextedit.html#styleSheet">styleSheet</a>(),
540<a name="x2070"></a> medit-&gt;<a href="qtextedit.html#mimeSourceFactory">mimeSourceFactory</a>(),
541 view.<a href="qrect.html#height">height</a>() );
542<a name="x2063"></a> richText.<a href="qsimplerichtext.html#setWidth">setWidth</a>( &amp;p, view.<a href="qrect.html#width">width</a>() );
543 int page = 1;
544 do {
545<a name="x2061"></a> richText.<a href="qsimplerichtext.html#draw">draw</a>( &amp;p, margin, margin, view, colorGroup() );
546<a name="x2057"></a> view.<a href="qrect.html#moveBy">moveBy</a>( 0, view.<a href="qrect.html#height">height</a>() );
547 p.<a href="qpainter.html#translate">translate</a>( 0 , -view.<a href="qrect.html#height">height</a>() );
548<a name="x2066"></a><a name="x2046"></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 ) ),
549 view.<a href="qrect.html#bottom">bottom</a>() + p.<a href="qpainter.html#fontMetrics">fontMetrics</a>().ascent() + 5, QString::number( page ) );
550<a name="x2062"></a> if ( view.<a href="qrect.html#top">top</a>() - margin &gt;= richText.<a href="qsimplerichtext.html#height">height</a>() )
551 break;
552 <a href="qstring.html">QString</a> msg( "Printing (page " );
553 msg += QString::<a href="qstring.html#number">number</a>( ++pageNo );
554 msg += ")...";
555 emit message( msg, 0 );
556<a name="x2050"></a> printer-&gt;<a href="qprinter.html#newPage">newPage</a>();
557 page++;
558 } while (TRUE);
559 }
560#endif
561}
562</pre>
563
564<p> <hr>
565<p> Main:
566<p> <pre>/****************************************************************************
567** $Id: mdi-example.html 2051 2007-02-21 10:04:20Z chehrlic $
568**
569** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
570**
571** This file is part of an example program for Qt. This example
572** program may be used, distributed and modified without limitation.
573**
574*****************************************************************************/
575
576#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
577#include "application.h"
578
579int main( int argc, char ** argv ) {
580 <a href="qapplication.html">QApplication</a> a( argc, argv );
581 ApplicationWindow * mw = new ApplicationWindow();
582 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>(mw);
583 mw-&gt;<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Multiple Documents Interface (MDI)" );
584 mw-&gt;<a href="qwidget.html#show">show</a>();
585<a name="x2097"></a><a name="x2096"></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>()) );
586 int res = a.<a href="qapplication.html#exec">exec</a>();
587 return res;
588}
589</pre>
590
591<p>See also <a href="examples.html">Examples</a>.
592
593<!-- eof -->
594<p><address><hr><div align=center>
595<table width=100% cellspacing=0 border=0><tr>
596<td>Copyright &copy; 2007
597<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
598<td align=right><div align=right>Qt 3.3.8</div>
599</table></div></address></body>
600</html>
Note: See TracBrowser for help on using the repository browser.