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/sdi/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/sdi/mainwindow.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2004-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"
52
MainWindow::MainWindow()
55
setCurrentFile("");
58
MainWindow::MainWindow(const QString &fileName)
64
void MainWindow::closeEvent(QCloseEvent *event)
74
void MainWindow::newFile()
76
MainWindow *other = new MainWindow;
77
other->move(x() + 40, y() + 40);
81
void MainWindow::open()
83
QString fileName = QFileDialog::getOpenFileName(this);
84
if (!fileName.isEmpty()) {
85
MainWindow *existing = findMainWindow(fileName);
89
existing->activateWindow();
93
if (isUntitled && textEdit->document()->isEmpty()
94
&& !isWindowModified()) {
97
MainWindow *other = new MainWindow(fileName);
98
if (other->isUntitled) {
102
other->move(x() + 40, y() + 40);
108
bool MainWindow::save()
113
return saveFile(curFile);
117
bool MainWindow::saveAs()
119
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
121
if (fileName.isEmpty())
124
return saveFile(fileName);
127
void MainWindow::about()
129
QMessageBox::about(this, tr("About SDI"),
130
tr("The <b>SDI</b> example demonstrates how to write single "
131
"document interface applications using Qt."));
134
void MainWindow::documentWasModified()
136
setWindowModified(true);
139
void MainWindow::init()
141
setAttribute(Qt::WA_DeleteOnClose);
145
textEdit = new QTextEdit;
146
setCentralWidget(textEdit);
155
connect(textEdit->document(), SIGNAL(contentsChanged()),
156
this, SLOT(documentWasModified()));
159
void MainWindow::createActions()
161
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
162
newAct->setShortcut(tr("Ctrl+N"));
163
newAct->setStatusTip(tr("Create a new file"));
164
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
166
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
167
openAct->setShortcut(tr("Ctrl+O"));
168
openAct->setStatusTip(tr("Open an existing file"));
169
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
171
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
172
saveAct->setShortcut(tr("Ctrl+S"));
173
saveAct->setStatusTip(tr("Save the document to disk"));
174
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
176
saveAsAct = new QAction(tr("Save &As..."), this);
177
saveAsAct->setStatusTip(tr("Save the document under a new name"));
178
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
180
closeAct = new QAction(tr("&Close"), this);
181
closeAct->setShortcut(tr("Ctrl+W"));
182
closeAct->setStatusTip(tr("Close this window"));
183
connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));
185
exitAct = new QAction(tr("E&xit"), this);
186
exitAct->setShortcut(tr("Ctrl+Q"));
187
exitAct->setStatusTip(tr("Exit the application"));
188
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
190
cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
191
cutAct->setShortcut(tr("Ctrl+X"));
192
cutAct->setStatusTip(tr("Cut the current selection's contents to the "
193
"clipboard"));
194
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
196
copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
197
copyAct->setShortcut(tr("Ctrl+C"));
198
copyAct->setStatusTip(tr("Copy the current selection's contents to the "
199
"clipboard"));
200
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
202
pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
203
pasteAct->setShortcut(tr("Ctrl+V"));
204
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
205
"selection"));
206
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
208
aboutAct = new QAction(tr("&About"), this);
209
aboutAct->setStatusTip(tr("Show the application's About box"));
210
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
212
aboutQtAct = new QAction(tr("About &Qt"), this);
213
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
214
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
217
void MainWindow::createMenus()
219
fileMenu = menuBar()->addMenu(tr("&File"));
220
fileMenu->addAction(newAct);
221
fileMenu->addAction(openAct);
222
fileMenu->addAction(saveAct);
223
fileMenu->addAction(saveAsAct);
224
fileMenu->addSeparator();
225
fileMenu->addAction(closeAct);
226
fileMenu->addAction(exitAct);
228
editMenu = menuBar()->addMenu(tr("&Edit"));
229
editMenu->addAction(cutAct);
230
editMenu->addAction(copyAct);
231
editMenu->addAction(pasteAct);
233
menuBar()->addSeparator();
235
helpMenu = menuBar()->addMenu(tr("&Help"));
236
helpMenu->addAction(aboutAct);
237
helpMenu->addAction(aboutQtAct);
240
void MainWindow::createToolBars()
242
fileToolBar = addToolBar(tr("File"));
243
fileToolBar->addAction(newAct);
244
fileToolBar->addAction(openAct);
245
fileToolBar->addAction(saveAct);
247
editToolBar = addToolBar(tr("Edit"));
248
editToolBar->addAction(cutAct);
249
editToolBar->addAction(copyAct);
250
editToolBar->addAction(pasteAct);
253
void MainWindow::createStatusBar()
255
statusBar()->showMessage(tr("Ready"));
258
void MainWindow::readSettings()
260
QSettings settings("Trolltech", "SDI Example");
261
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
262
QSize size = settings.value("size", QSize(400, 400)).toSize();
267
void MainWindow::writeSettings()
269
QSettings settings("Trolltech", "SDI Example");
270
settings.setValue("pos", pos());
271
settings.setValue("size", size());
274
bool MainWindow::maybeSave()
276
if (textEdit->document()->isModified()) {
277
int ret = QMessageBox::warning(this, tr("SDI"),
278
tr("The document has been modified.\n"
279
"Do you want to save your changes?"),
280
QMessageBox::Yes | QMessageBox::Default,
282
QMessageBox::Cancel | QMessageBox::Escape);
283
if (ret == QMessageBox::Yes)
285
else if (ret == QMessageBox::Cancel)
291
void MainWindow::loadFile(const QString &fileName)
294
QFile file(fileName);
295
if (!file.open(QFile::ReadOnly | QFile::Text)) {
296
QMessageBox::warning(this, tr("SDI"),
297
tr("Cannot read file %1:\n%2.")
299
.arg(file.errorString()));
303
QTextStream in(&file);
304
QApplication::setOverrideCursor(Qt::WaitCursor);
305
textEdit->setPlainText(in.readAll());
306
QApplication::restoreOverrideCursor();
308
setCurrentFile(fileName);
309
statusBar()->showMessage(tr("File loaded"), 2000);
312
bool MainWindow::saveFile(const QString &fileName)
314
QFile file(fileName);
315
if (!file.open(QFile::WriteOnly | QFile::Text)) {
316
QMessageBox::warning(this, tr("SDI"),
317
tr("Cannot write file %1:\n%2.")
319
.arg(file.errorString()));
323
QTextStream out(&file);
324
QApplication::setOverrideCursor(Qt::WaitCursor);
325
out << textEdit->toPlainText();
326
QApplication::restoreOverrideCursor();
328
setCurrentFile(fileName);
329
statusBar()->showMessage(tr("File saved"), 2000);
333
void MainWindow::setCurrentFile(const QString &fileName)
335
static int sequenceNumber = 1;
337
isUntitled = fileName.isEmpty();
339
curFile = tr("document%1.txt").arg(sequenceNumber++);
341
curFile = QFileInfo(fileName).canonicalFilePath();
344
textEdit->document()->setModified(false);
345
setWindowModified(false);
347
setWindowTitle(tr("%1[*] - %2").arg(strippedName(curFile))
348
.arg(tr("SDI")));
351
QString MainWindow::strippedName(const QString &fullFileName)
353
return QFileInfo(fullFileName).fileName();
356
MainWindow *MainWindow::findMainWindow(const QString &fileName)
358
QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
360
foreach (QWidget *widget, qApp->topLevelWidgets()) {
361
MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
362
if (mainWin && mainWin->curFile == canonicalFilePath)
367
<p /><address><hr /><div align="center">
368
<table width="100%" cellspacing="0" border="0"><tr class="address">
369
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
370
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
371
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
372
</tr></table></div></address></body>