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/recentfiles/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/recentfiles/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()
54
setAttribute(Qt::WA_DeleteOnClose);
56
textEdit = new QTextEdit;
57
setCentralWidget(textEdit);
63
setWindowTitle(tr("Recent Files"));
67
void MainWindow::newFile()
69
MainWindow *other = new MainWindow;
73
void MainWindow::open()
75
QString fileName = QFileDialog::getOpenFileName(this);
76
if (!fileName.isEmpty())
80
void MainWindow::save()
82
if (curFile.isEmpty())
88
void MainWindow::saveAs()
90
QString fileName = QFileDialog::getSaveFileName(this);
91
if (fileName.isEmpty())
94
if (QFile::exists(fileName)) {
95
int ret = QMessageBox::warning(this, tr("Recent Files"),
96
tr("File %1 already exists.\n"
97
"Do you want to overwrite it?")
98
.arg(QDir::convertSeparators(fileName)),
99
QMessageBox::Yes | QMessageBox::Default,
100
QMessageBox::No | QMessageBox::Escape);
101
if (ret == QMessageBox::No)
107
void MainWindow::openRecentFile()
109
QAction *action = qobject_cast<QAction *>(sender());
111
loadFile(action->data().toString());
114
void MainWindow::about()
116
QMessageBox::about(this, tr("About Recent Files"),
117
tr("The <b>Recent Files</b> example demonstrates how to provide a "
118
"recently used file menu in a Qt application."));
121
void MainWindow::createActions()
123
newAct = new QAction(tr("&New"), this);
124
newAct->setShortcut(tr("Ctrl+N"));
125
newAct->setStatusTip(tr("Create a new file"));
126
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
128
openAct = new QAction(tr("&Open..."), this);
129
openAct->setShortcut(tr("Ctrl+O"));
130
openAct->setStatusTip(tr("Open an existing file"));
131
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
133
saveAct = new QAction(tr("&Save"), this);
134
saveAct->setShortcut(tr("Ctrl+S"));
135
saveAct->setStatusTip(tr("Save the document to disk"));
136
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
138
saveAsAct = new QAction(tr("Save &As..."), this);
139
saveAsAct->setStatusTip(tr("Save the document under a new name"));
140
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
142
for (int i = 0; i < MaxRecentFiles; ++i) {
143
recentFileActs[i] = new QAction(this);
144
recentFileActs[i]->setVisible(false);
145
connect(recentFileActs[i], SIGNAL(triggered()),
146
this, SLOT(openRecentFile()));
149
exitAct = new QAction(tr("&Close"), this);
150
exitAct->setShortcut(tr("Ctrl+W"));
151
exitAct->setStatusTip(tr("Close this window"));
152
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
154
exitAct = new QAction(tr("E&xit"), this);
155
exitAct->setShortcut(tr("Ctrl+Q"));
156
exitAct->setStatusTip(tr("Exit the application"));
157
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
159
aboutAct = new QAction(tr("&About"), this);
160
aboutAct->setStatusTip(tr("Show the application's About box"));
161
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
163
aboutQtAct = new QAction(tr("About &Qt"), this);
164
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
165
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
168
void MainWindow::createMenus()
170
fileMenu = menuBar()->addMenu(tr("&File"));
171
fileMenu->addAction(newAct);
172
fileMenu->addAction(openAct);
173
fileMenu->addAction(saveAct);
174
fileMenu->addAction(saveAsAct);
175
separatorAct = fileMenu->addSeparator();
176
for (int i = 0; i < MaxRecentFiles; ++i)
177
fileMenu->addAction(recentFileActs[i]);
178
fileMenu->addSeparator();
179
fileMenu->addAction(exitAct);
180
updateRecentFileActions();
182
menuBar()->addSeparator();
184
helpMenu = menuBar()->addMenu(tr("&Help"));
185
helpMenu->addAction(aboutAct);
186
helpMenu->addAction(aboutQtAct);
189
void MainWindow::loadFile(const QString &fileName)
191
QFile file(fileName);
192
if (!file.open(QFile::ReadOnly | QFile::Text)) {
193
QMessageBox::warning(this, tr("Recent Files"),
194
tr("Cannot read file %1:\n%2.")
196
.arg(file.errorString()));
200
QTextStream in(&file);
201
QApplication::setOverrideCursor(Qt::WaitCursor);
202
textEdit->setPlainText(in.readAll());
203
QApplication::restoreOverrideCursor();
205
setCurrentFile(fileName);
206
statusBar()->showMessage(tr("File loaded"), 2000);
209
void MainWindow::saveFile(const QString &fileName)
211
QFile file(fileName);
212
if (!file.open(QFile::WriteOnly | QFile::Text)) {
213
QMessageBox::warning(this, tr("Recent Files"),
214
tr("Cannot write file %1:\n%2.")
216
.arg(file.errorString()));
220
QTextStream out(&file);
221
QApplication::setOverrideCursor(Qt::WaitCursor);
222
out << textEdit->toPlainText();
223
QApplication::restoreOverrideCursor();
225
setCurrentFile(fileName);
226
statusBar()->showMessage(tr("File saved"), 2000);
229
void MainWindow::setCurrentFile(const QString &fileName)
232
if (curFile.isEmpty())
233
setWindowTitle(tr("Recent Files"));
235
setWindowTitle(tr("%1 - %2").arg(strippedName(curFile))
236
.arg(tr("Recent Files")));
238
QSettings settings("Trolltech", "Recent Files Example");
239
QStringList files = settings.value("recentFileList").toStringList();
240
files.removeAll(fileName);
241
files.prepend(fileName);
242
while (files.size() > MaxRecentFiles)
245
settings.setValue("recentFileList", files);
247
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
248
MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
250
mainWin->updateRecentFileActions();
254
void MainWindow::updateRecentFileActions()
256
QSettings settings("Trolltech", "Recent Files Example");
257
QStringList files = settings.value("recentFileList").toStringList();
259
int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
261
for (int i = 0; i < numRecentFiles; ++i) {
262
QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
263
recentFileActs[i]->setText(text);
264
recentFileActs[i]->setData(files[i]);
265
recentFileActs[i]->setVisible(true);
267
for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
268
recentFileActs[j]->setVisible(false);
270
separatorAct->setVisible(numRecentFiles > 0);
273
QString MainWindow::strippedName(const QString &fullFileName)
275
return QFileInfo(fullFileName).fileName();
277
<p /><address><hr /><div align="center">
278
<table width="100%" cellspacing="0" border="0"><tr class="address">
279
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
280
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
281
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
282
</tr></table></div></address></body>