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"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>An MDI Application</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example program is just like the application example, except
|
---|
37 | that 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 <<a href="qmainwindow-h.html">qmainwindow.h</a>>
|
---|
54 | #include <<a href="qptrlist-h.html">qptrlist.h</a>>
|
---|
55 |
|
---|
56 | class QTextEdit;
|
---|
57 | class QToolBar;
|
---|
58 | class QPopupMenu;
|
---|
59 | class QWorkspace;
|
---|
60 | class QPopupMenu;
|
---|
61 | class QMovie;
|
---|
62 |
|
---|
63 | class MDIWindow: public <a href="qmainwindow.html">QMainWindow</a>
|
---|
64 | {
|
---|
65 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
66 | public:
|
---|
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>& fn );
|
---|
71 | void save();
|
---|
72 | void saveAs();
|
---|
73 | void print( <a href="qprinter.html">QPrinter</a>* );
|
---|
74 |
|
---|
75 | protected:
|
---|
76 | void closeEvent( <a href="qcloseevent.html">QCloseEvent</a> * );
|
---|
77 |
|
---|
78 | signals:
|
---|
79 | void message(const <a href="qstring.html">QString</a>&, int );
|
---|
80 |
|
---|
81 | private:
|
---|
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 |
|
---|
88 | class ApplicationWindow: public <a href="qmainwindow.html">QMainWindow</a>
|
---|
89 | {
|
---|
90 | Q_OBJECT
|
---|
91 | public:
|
---|
92 | ApplicationWindow();
|
---|
93 | ~ApplicationWindow();
|
---|
94 |
|
---|
95 | protected:
|
---|
96 | void closeEvent( <a href="qcloseevent.html">QCloseEvent</a> * );
|
---|
97 |
|
---|
98 | private 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 |
|
---|
113 | private:
|
---|
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 <<a href="qworkspace-h.html">qworkspace.h</a>>
|
---|
138 | #include <<a href="qimage-h.html">qimage.h</a>>
|
---|
139 | #include <<a href="qpixmap-h.html">qpixmap.h</a>>
|
---|
140 | #include <<a href="qtoolbar-h.html">qtoolbar.h</a>>
|
---|
141 | #include <<a href="qtoolbutton-h.html">qtoolbutton.h</a>>
|
---|
142 | #include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
|
---|
143 | #include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
---|
144 | #include <<a href="qmovie-h.html">qmovie.h</a>>
|
---|
145 | #include <<a href="qfile-h.html">qfile.h</a>>
|
---|
146 | #include <<a href="qfiledialog-h.html">qfiledialog.h</a>>
|
---|
147 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
148 | #include <<a href="qstatusbar-h.html">qstatusbar.h</a>>
|
---|
149 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
150 | #include <<a href="qprinter-h.html">qprinter.h</a>>
|
---|
151 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
152 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
153 | #include <<a href="qaccel-h.html">qaccel.h</a>>
|
---|
154 | #include <<a href="qtextstream-h.html">qtextstream.h</a>>
|
---|
155 | #include <<a href="qtextedit-h.html">qtextedit.h</a>>
|
---|
156 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
157 | #include <<a href="qpaintdevicemetrics-h.html">qpaintdevicemetrics.h</a>>
|
---|
158 | #include <<a href="qwhatsthis-h.html">qwhatsthis.h</a>>
|
---|
159 | #include <<a href="qobjectlist-h.html">qobjectlist.h</a>>
|
---|
160 | #include <<a href="qvbox-h.html">qvbox.h</a>>
|
---|
161 | #include <<a href="qsimplerichtext-h.html">qsimplerichtext.h</a>>
|
---|
162 |
|
---|
163 | #include "filesave.xpm"
|
---|
164 | #include "fileopen.xpm"
|
---|
165 | #include "fileprint.xpm"
|
---|
166 |
|
---|
167 |
|
---|
168 | const char * fileOpenText = "Click this button to open a <em>new file</em>. <br><br>"
|
---|
169 | "You can also select the <b>Open command</b> from the File menu.";
|
---|
170 | const 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.";
|
---|
174 | const 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>()->insertItem( "&File", file );
|
---|
216 |
|
---|
217 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "&New", this, SLOT(newDoc()), CTRL+Key_N );
|
---|
218 |
|
---|
219 | id = file-><a href="qmenudata.html#insertItem">insertItem</a>( openIcon, "&Open...",
|
---|
220 | this, SLOT(load()), CTRL+Key_O );
|
---|
221 | <a name="x2035"></a> file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileOpenText );
|
---|
222 |
|
---|
223 | id = file-><a href="qmenudata.html#insertItem">insertItem</a>( saveIcon, "&Save",
|
---|
224 | this, SLOT(save()), CTRL+Key_S );
|
---|
225 | file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );
|
---|
226 | id = file-><a href="qmenudata.html#insertItem">insertItem</a>( "Save &As...", this, SLOT(saveAs()) );
|
---|
227 | file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );
|
---|
228 | #ifndef QT_NO_PRINTER
|
---|
229 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
230 | id = file-><a href="qmenudata.html#insertItem">insertItem</a>( printIcon, "&Print...",
|
---|
231 | this, SLOT(print()), CTRL+Key_P );
|
---|
232 | file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, filePrintText );
|
---|
233 | #endif
|
---|
234 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
235 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Close", this, SLOT(closeWindow()), CTRL+Key_W );
|
---|
236 | <a name="x2020"></a> file-><a href="qmenudata.html#insertItem">insertItem</a>( "&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-><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>()->insertItem( "&Windows", windowsMenu );
|
---|
243 |
|
---|
244 | <a href="qmainwindow.html#menuBar">menuBar</a>()->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>()->insertItem( "&Help", help );
|
---|
247 |
|
---|
248 | help-><a href="qmenudata.html#insertItem">insertItem</a>( "&About", this, SLOT(about()), Key_F1);
|
---|
249 | help-><a href="qmenudata.html#insertItem">insertItem</a>( "About &Qt", this, SLOT(aboutQt()));
|
---|
250 | help-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
251 | help-><a href="qmenudata.html#insertItem">insertItem</a>( "What's &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-><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-><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>()->message( "Ready", 2000 );
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | ApplicationWindow::~ApplicationWindow()
|
---|
264 | {
|
---|
265 | #ifndef QT_NO_PRINTER
|
---|
266 | delete printer;
|
---|
267 | #endif
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 |
|
---|
272 | MDIWindow* <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>&, int) ), statusBar(), SLOT( message(const <a href="qstring.html">QString</a>&, int )) );
|
---|
276 | w-><a href="qwidget.html#setCaption">setCaption</a>("unnamed document");
|
---|
277 | <a name="x2085"></a> w-><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-><a href="qworkspace.html#windowList">windowList</a>().isEmpty() )
|
---|
280 | <a name="x2087"></a> w-><a href="qwidget.html#showMaximized">showMaximized</a>();
|
---|
281 | else
|
---|
282 | w-><a href="qwidget.html#show">show</a>();
|
---|
283 | return w;
|
---|
284 | }
|
---|
285 |
|
---|
286 | void <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->load( fn );
|
---|
292 | } else {
|
---|
293 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Loading aborted", 2000 );
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | void <a name="f538"></a>ApplicationWindow::save()
|
---|
298 | {
|
---|
299 | <a name="x2090"></a> MDIWindow* m = (MDIWindow*)ws-><a href="qworkspace.html#activeWindow">activeWindow</a>();
|
---|
300 | if ( m )
|
---|
301 | m->save();
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | void <a name="f539"></a>ApplicationWindow::saveAs()
|
---|
306 | {
|
---|
307 | MDIWindow* m = (MDIWindow*)ws-><a href="qworkspace.html#activeWindow">activeWindow</a>();
|
---|
308 | if ( m )
|
---|
309 | m->saveAs();
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | void <a name="f540"></a>ApplicationWindow::print()
|
---|
314 | {
|
---|
315 | #ifndef QT_NO_PRINTER
|
---|
316 | MDIWindow* m = (MDIWindow*)ws-><a href="qworkspace.html#activeWindow">activeWindow</a>();
|
---|
317 | if ( m )
|
---|
318 | m->print( printer );
|
---|
319 | #endif
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | void <a name="f541"></a>ApplicationWindow::closeWindow()
|
---|
324 | {
|
---|
325 | MDIWindow* m = (MDIWindow*)ws-><a href="qworkspace.html#activeWindow">activeWindow</a>();
|
---|
326 | if ( m )
|
---|
327 | <a name="x2076"></a> m-><a href="qwidget.html#close">close</a>();
|
---|
328 | }
|
---|
329 |
|
---|
330 | void <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 |
|
---|
338 | void <a name="f543"></a>ApplicationWindow::aboutQt()
|
---|
339 | {
|
---|
340 | QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Qt Application Example" );
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | void <a name="f544"></a>ApplicationWindow::windowsMenuAboutToShow()
|
---|
345 | {
|
---|
346 | <a name="x2029"></a> windowsMenu-><a href="qmenudata.html#clear">clear</a>();
|
---|
347 | <a name="x2091"></a> int cascadeId = windowsMenu-><a href="qmenudata.html#insertItem">insertItem</a>("&Cascade", ws, SLOT(<a href="qworkspace.html#cascade">cascade</a>() ) );
|
---|
348 | <a name="x2093"></a> int tileId = windowsMenu-><a href="qmenudata.html#insertItem">insertItem</a>("&Tile", ws, SLOT(<a href="qworkspace.html#tile">tile</a>() ) );
|
---|
349 | int horTileId = windowsMenu-><a href="qmenudata.html#insertItem">insertItem</a>("Tile &Horizontally", this, SLOT(tileHorizontal() ) );
|
---|
350 | if ( ws-><a href="qworkspace.html#windowList">windowList</a>().isEmpty() ) {
|
---|
351 | <a name="x2033"></a> windowsMenu-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( cascadeId, FALSE );
|
---|
352 | windowsMenu-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( tileId, FALSE );
|
---|
353 | windowsMenu-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( horTileId, FALSE );
|
---|
354 | }
|
---|
355 | windowsMenu-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
356 | QWidgetList windows = ws-><a href="qworkspace.html#windowList">windowList</a>();
|
---|
357 | <a name="x2054"></a> for ( int i = 0; i < int(windows.count()); ++i ) {
|
---|
358 | <a name="x2053"></a> int id = windowsMenu-><a href="qmenudata.html#insertItem">insertItem</a>(windows.at(i)->caption(),
|
---|
359 | this, SLOT( windowsMenuActivated( int ) ) );
|
---|
360 | <a name="x2034"></a> windowsMenu-><a href="qmenudata.html#setItemParameter">setItemParameter</a>( id, i );
|
---|
361 | <a name="x2032"></a> windowsMenu-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( id, ws-><a href="qworkspace.html#activeWindow">activeWindow</a>() == windows.at(i) );
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 | void <a name="f545"></a>ApplicationWindow::windowsMenuActivated( int id )
|
---|
366 | {
|
---|
367 | <a href="qwidget.html">QWidget</a>* w = ws-><a href="qworkspace.html#windowList">windowList</a>().at( id );
|
---|
368 | if ( w )
|
---|
369 | <a name="x2088"></a> w-><a href="qwidget.html#showNormal">showNormal</a>();
|
---|
370 | <a name="x2084"></a> w-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
371 | }
|
---|
372 |
|
---|
373 | void <a name="f546"></a>ApplicationWindow::tileHorizontal()
|
---|
374 | {
|
---|
375 | // primitive horizontal tiling
|
---|
376 | QWidgetList windows = ws-><a href="qworkspace.html#windowList">windowList</a>();
|
---|
377 | if ( !windows.count() )
|
---|
378 | return;
|
---|
379 |
|
---|
380 | if (ws-><a href="qworkspace.html#activeWindow">activeWindow</a>())
|
---|
381 | ws-><a href="qworkspace.html#activeWindow">activeWindow</a>()->showNormal();
|
---|
382 |
|
---|
383 | <a name="x2078"></a> int heightForEach = ws-><a href="qwidget.html#height">height</a>() / windows.count();
|
---|
384 | int y = 0;
|
---|
385 | for ( int i = 0; i < 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-><a href="qwidget.html#minimumHeight">minimumHeight</a>()+window-><a href="qwidget.html#parentWidget">parentWidget</a>()->baseSize().height();
|
---|
388 | int actHeight = QMAX(heightForEach, preferredHeight);
|
---|
389 |
|
---|
390 | <a name="x2089"></a> window-><a href="qwidget.html#parentWidget">parentWidget</a>()->setGeometry( 0, y, ws-><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-><a href="qworkspace.html#windowList">windowList</a>();
|
---|
398 | if ( windows.count() ) {
|
---|
399 | for ( int i = 0; i < int(windows.count()); ++i ) {
|
---|
400 | <a href="qwidget.html">QWidget</a> *window = windows.at( i );
|
---|
401 | if ( !window-><a href="qwidget.html#close">close</a>() ) {
|
---|
402 | <a name="x2022"></a> e-><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 |
|
---|
420 | MDIWindow::~MDIWindow()
|
---|
421 | {
|
---|
422 | delete mmovie;
|
---|
423 | }
|
---|
424 |
|
---|
425 | void MDIWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a> *e )
|
---|
426 | {
|
---|
427 | <a name="x2069"></a> if ( medit-><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-><a href="qcloseevent.html#accept">accept</a>();
|
---|
436 | else
|
---|
437 | e-><a href="qcloseevent.html#ignore">ignore</a>();
|
---|
438 | }
|
---|
439 | break;
|
---|
440 | case 1:
|
---|
441 | e-><a href="qcloseevent.html#accept">accept</a>();
|
---|
442 | break;
|
---|
443 | default:
|
---|
444 | e-><a href="qcloseevent.html#ignore">ignore</a>();
|
---|
445 | break;
|
---|
446 | }
|
---|
447 | } else {
|
---|
448 | e-><a href="qcloseevent.html#accept">accept</a>();
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | void <a name="f548"></a>MDIWindow::load( const <a href="qstring.html">QString</a>& 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-><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->setDisplayWidget(tmp);
|
---|
468 | #endif
|
---|
469 | <a name="x2082"></a> tmp-><a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>(QWidget::NoBackground);
|
---|
470 | tmp-><a href="qwidget.html#show">show</a>();
|
---|
471 | mmovie=qm;
|
---|
472 | } else {
|
---|
473 | mmovie = 0;
|
---|
474 |
|
---|
475 | <a href="qtextstream.html">QTextStream</a> t(&f);
|
---|
476 | <a name="x2074"></a> <a href="qstring.html">QString</a> s = t.<a href="qtextstream.html#read">read</a>();
|
---|
477 | medit-><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 |
|
---|
486 | void <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-><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( &f );
|
---|
502 | t << 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 |
|
---|
510 | void <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 |
|
---|
521 | void <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-><a href="qprinter.html#setup">setup</a>(this) ) { // printer dialog
|
---|
527 | <a name="x2051"></a> printer-><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-><a href="qtextedit.html#text">text</a>()),
|
---|
537 | QFont(),
|
---|
538 | <a name="x2068"></a> medit-><a href="qtextedit.html#context">context</a>(),
|
---|
539 | <a name="x2072"></a> medit-><a href="qtextedit.html#styleSheet">styleSheet</a>(),
|
---|
540 | <a name="x2070"></a> medit-><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>( &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>( &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 >= 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-><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 <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
577 | #include "application.h"
|
---|
578 |
|
---|
579 | int 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-><a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Multiple Documents Interface (MDI)" );
|
---|
584 | mw-><a href="qwidget.html#show">show</a>();
|
---|
585 | <a name="x2097"></a><a name="x2096"></a> a.<a href="qobject.html#connect">connect</a>( &a, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), &a, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
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 © 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>
|
---|