1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: mainwindow.cpp Example File (mainwindows/mdi/mainwindow.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">mainwindow.cpp Example File<br /><small><small>mainwindows/mdi/mainwindow.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
50
#include "mainwindow.h"
51
#include "mdichild.h"
53
MainWindow::MainWindow()
55
workspace = new QWorkspace;
56
setCentralWidget(workspace);
57
connect(workspace, SIGNAL(windowActivated(QWidget *)),
58
this, SLOT(updateMenus()));
59
windowMapper = new QSignalMapper(this);
60
connect(windowMapper, SIGNAL(mapped(QWidget *)),
61
workspace, SLOT(setActiveWindow(QWidget *)));
71
setWindowTitle(tr("MDI"));
74
void MainWindow::closeEvent(QCloseEvent *event)
76
workspace->closeAllWindows();
77
if (activeMdiChild()) {
85
void MainWindow::newFile()
87
MdiChild *child = createMdiChild();
92
void MainWindow::open()
94
QString fileName = QFileDialog::getOpenFileName(this);
95
if (!fileName.isEmpty()) {
96
MdiChild *existing = findMdiChild(fileName);
98
workspace->setActiveWindow(existing);
102
MdiChild *child = createMdiChild();
103
if (child->loadFile(fileName)) {
104
statusBar()->showMessage(tr("File loaded"), 2000);
112
void MainWindow::save()
114
if (activeMdiChild()->save())
115
statusBar()->showMessage(tr("File saved"), 2000);
118
void MainWindow::saveAs()
120
if (activeMdiChild()->saveAs())
121
statusBar()->showMessage(tr("File saved"), 2000);
124
void MainWindow::cut()
126
activeMdiChild()->cut();
129
void MainWindow::copy()
131
activeMdiChild()->copy();
134
void MainWindow::paste()
136
activeMdiChild()->paste();
139
void MainWindow::about()
141
QMessageBox::about(this, tr("About MDI"),
142
tr("The <b>MDI</b> example demonstrates how to write multiple "
143
"document interface applications using Qt."));
146
void MainWindow::updateMenus()
148
bool hasMdiChild = (activeMdiChild() != 0);
149
saveAct->setEnabled(hasMdiChild);
150
saveAsAct->setEnabled(hasMdiChild);
151
pasteAct->setEnabled(hasMdiChild);
152
closeAct->setEnabled(hasMdiChild);
153
closeAllAct->setEnabled(hasMdiChild);
154
tileAct->setEnabled(hasMdiChild);
155
cascadeAct->setEnabled(hasMdiChild);
156
nextAct->setEnabled(hasMdiChild);
157
previousAct->setEnabled(hasMdiChild);
158
separatorAct->setVisible(hasMdiChild);
160
bool hasSelection = (activeMdiChild() &&
161
activeMdiChild()->textCursor().hasSelection());
162
cutAct->setEnabled(hasSelection);
163
copyAct->setEnabled(hasSelection);
166
void MainWindow::updateWindowMenu()
168
windowMenu->clear();
169
windowMenu->addAction(closeAct);
170
windowMenu->addAction(closeAllAct);
171
windowMenu->addSeparator();
172
windowMenu->addAction(tileAct);
173
windowMenu->addAction(cascadeAct);
174
windowMenu->addSeparator();
175
windowMenu->addAction(nextAct);
176
windowMenu->addAction(previousAct);
177
windowMenu->addAction(separatorAct);
179
QList<QWidget *> windows = workspace->windowList();
180
separatorAct->setVisible(!windows.isEmpty());
182
for (int i = 0; i < windows.size(); ++i) {
183
MdiChild *child = qobject_cast<MdiChild *>(windows.at(i));
187
text = tr("&%1. %2").arg(i + 1)
188
.arg(child->userFriendlyCurrentFile());
190
text = tr("%1. %2").arg(i + 1)
191
.arg(child->userFriendlyCurrentFile());
193
QAction *action = windowMenu->addAction(text);
194
action->setCheckable(true);
195
action ->setChecked(child == activeMdiChild());
196
connect(action, SIGNAL(triggered()), windowMapper, SLOT(map()));
197
windowMapper->setMapping(action, child);
201
MdiChild *MainWindow::createMdiChild()
203
MdiChild *child = new MdiChild;
204
workspace->addWindow(child);
206
connect(child, SIGNAL(copyAvailable(bool)),
207
cutAct, SLOT(setEnabled(bool)));
208
connect(child, SIGNAL(copyAvailable(bool)),
209
copyAct, SLOT(setEnabled(bool)));
214
void MainWindow::createActions()
216
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
217
newAct->setShortcut(tr("Ctrl+N"));
218
newAct->setStatusTip(tr("Create a new file"));
219
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
221
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
222
openAct->setShortcut(tr("Ctrl+O"));
223
openAct->setStatusTip(tr("Open an existing file"));
224
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
226
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
227
saveAct->setShortcut(tr("Ctrl+S"));
228
saveAct->setStatusTip(tr("Save the document to disk"));
229
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
231
saveAsAct = new QAction(tr("Save &As..."), this);
232
saveAsAct->setStatusTip(tr("Save the document under a new name"));
233
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
235
exitAct = new QAction(tr("E&xit"), this);
236
exitAct->setShortcut(tr("Ctrl+Q"));
237
exitAct->setStatusTip(tr("Exit the application"));
238
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
240
cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
241
cutAct->setShortcut(tr("Ctrl+X"));
242
cutAct->setStatusTip(tr("Cut the current selection's contents to the "
243
"clipboard"));
244
connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
246
copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
247
copyAct->setShortcut(tr("Ctrl+C"));
248
copyAct->setStatusTip(tr("Copy the current selection's contents to the "
249
"clipboard"));
250
connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
252
pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
253
pasteAct->setShortcut(tr("Ctrl+V"));
254
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
255
"selection"));
256
connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
258
closeAct = new QAction(tr("Cl&ose"), this);
259
closeAct->setShortcut(tr("Ctrl+F4"));
260
closeAct->setStatusTip(tr("Close the active window"));
261
connect(closeAct, SIGNAL(triggered()),
262
workspace, SLOT(closeActiveWindow()));
264
closeAllAct = new QAction(tr("Close &All"), this);
265
closeAllAct->setStatusTip(tr("Close all the windows"));
266
connect(closeAllAct, SIGNAL(triggered()),
267
workspace, SLOT(closeAllWindows()));
269
tileAct = new QAction(tr("&Tile"), this);
270
tileAct->setStatusTip(tr("Tile the windows"));
271
connect(tileAct, SIGNAL(triggered()), workspace, SLOT(tile()));
273
cascadeAct = new QAction(tr("&Cascade"), this);
274
cascadeAct->setStatusTip(tr("Cascade the windows"));
275
connect(cascadeAct, SIGNAL(triggered()), workspace, SLOT(cascade()));
277
nextAct = new QAction(tr("Ne&xt"), this);
278
nextAct->setShortcut(tr("Ctrl+F6"));
279
nextAct->setStatusTip(tr("Move the focus to the next window"));
280
connect(nextAct, SIGNAL(triggered()),
281
workspace, SLOT(activateNextWindow()));
283
previousAct = new QAction(tr("Pre&vious"), this);
284
previousAct->setShortcut(tr("Ctrl+Shift+F6"));
285
previousAct->setStatusTip(tr("Move the focus to the previous "
286
"window"));
287
connect(previousAct, SIGNAL(triggered()),
288
workspace, SLOT(activatePreviousWindow()));
290
separatorAct = new QAction(this);
291
separatorAct->setSeparator(true);
293
aboutAct = new QAction(tr("&About"), this);
294
aboutAct->setStatusTip(tr("Show the application's About box"));
295
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
297
aboutQtAct = new QAction(tr("About &Qt"), this);
298
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
299
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
302
void MainWindow::createMenus()
304
fileMenu = menuBar()->addMenu(tr("&File"));
305
fileMenu->addAction(newAct);
306
fileMenu->addAction(openAct);
307
fileMenu->addAction(saveAct);
308
fileMenu->addAction(saveAsAct);
309
fileMenu->addSeparator();
310
fileMenu->addAction(exitAct);
312
editMenu = menuBar()->addMenu(tr("&Edit"));
313
editMenu->addAction(cutAct);
314
editMenu->addAction(copyAct);
315
editMenu->addAction(pasteAct);
317
windowMenu = menuBar()->addMenu(tr("&Window"));
318
connect(windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowMenu()));
320
menuBar()->addSeparator();
322
helpMenu = menuBar()->addMenu(tr("&Help"));
323
helpMenu->addAction(aboutAct);
324
helpMenu->addAction(aboutQtAct);
327
void MainWindow::createToolBars()
329
fileToolBar = addToolBar(tr("File"));
330
fileToolBar->addAction(newAct);
331
fileToolBar->addAction(openAct);
332
fileToolBar->addAction(saveAct);
334
editToolBar = addToolBar(tr("Edit"));
335
editToolBar->addAction(cutAct);
336
editToolBar->addAction(copyAct);
337
editToolBar->addAction(pasteAct);
340
void MainWindow::createStatusBar()
342
statusBar()->showMessage(tr("Ready"));
345
void MainWindow::readSettings()
347
QSettings settings("Trolltech", "MDI Example");
348
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
349
QSize size = settings.value("size", QSize(400, 400)).toSize();
354
void MainWindow::writeSettings()
356
QSettings settings("Trolltech", "MDI Example");
357
settings.setValue("pos", pos());
358
settings.setValue("size", size());
361
MdiChild *MainWindow::activeMdiChild()
363
return qobject_cast<MdiChild *>(workspace->activeWindow());
366
MdiChild *MainWindow::findMdiChild(const QString &fileName)
368
QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
370
foreach (QWidget *window, workspace->windowList()) {
371
MdiChild *mdiChild = qobject_cast<MdiChild *>(window);
372
if (mdiChild->currentFile() == canonicalFilePath)
377
<p /><address><hr /><div align="center">
378
<table width="100%" cellspacing="0" border="0"><tr class="address">
379
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
380
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
381
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
382
</tr></table></div></address></body>