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">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/application.qdoc -->
7
<title>Qt 4.0: Application Example</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
19
<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>
20
<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">Application Example</h1>
23
<li><a href="mainwindows-application-mainwindow-cpp.html">mainwindows/application/mainwindow.cpp</a></li>
24
<li><a href="mainwindows-application-mainwindow-h.html">mainwindows/application/mainwindow.h</a></li>
25
<li><a href="mainwindows-application-main-cpp.html">mainwindows/application/main.cpp</a></li>
26
<li><a href="mainwindows-application-application-qrc.html">mainwindows/application/application.qrc</a></li>
28
<p>The Application example shows how to implement a standard GUI application with menus, toolbars, and a status bar. The example itself is a simple text editor program built around <a href="qtextedit.html">QTextEdit</a>.</p>
29
<center><img src="images/application.png" alt="Screenshot of the Application example" /></center><p>Nearly all of the code for the Application example is in the <tt>MainWindow</tt> class, which inherits <a href="qmainwindow.html">QMainWindow</a>. <a href="qmainwindow.html">QMainWindow</a> provides the framework for windows that have menus, toolbars, dock windows, and a status bar. The application provides <b>File</b>, <b>Edit</b>, and <b>Help</b> entries in the menu bar, with the following popup menus:</p>
30
<center><img src="images/application-menus.png" alt="The Application example's menu system" /></center><p>The status bar at the bottom of the main window shows a description of the menu item or toolbar button under the cursor.</p>
31
<p>To keep the example simple, recently opened files aren't shown in the <b>File</b> menu, even though this feature is desired in 90% of applications. The <a href="mainwindows-recentfiles.html">Recent Files</a> example shows how to implement this. Furthermore, this example can only load one file at a time. The <a href="mainwindows-sdi.html">SDI</a> and <a href="mainwindows-mdi.html">MDI</a> examples shows how to lift these restrictions.</p>
32
<a name="mainwindow-class-definition"></a>
33
<h2>MainWindow Class Definition</h2>
34
<p>Here's the class definition:</p>
35
<pre> class MainWindow : public QMainWindow
43
void closeEvent(QCloseEvent *event);
51
void documentWasModified();
56
void createToolBars();
57
void createStatusBar();
61
void loadFile(const QString &fileName);
62
bool saveFile(const QString &fileName);
63
void setCurrentFile(const QString &fileName);
64
QString strippedName(const QString &fullFileName);
72
QToolBar *fileToolBar;
73
QToolBar *editToolBar;
85
<p>The public API is restricted to the constructor. In the <tt>protected</tt> section, we reimplement <a href="qwidget.html#closeEvent">QWidget::closeEvent</a>() to detect when the user attempts to close the window, and warn the user about unsaved changes. In the <tt>private slots</tt> section, we declare slots that correspond to menu entries, as well as a mysterious <tt>documentWasModified()</tt> slot. Finally, in the <tt>private</tt> section of the class, we have various members that will be explained in due time.</p>
86
<a name="mainwindow-class-implementation"></a>
87
<h2>MainWindow Class Implementation</h2>
88
<pre> #include <QtGui>
90
#include "mainwindow.h"</pre>
91
<p>We start by including <tt><QtGui></tt>, a header file that contains the definition of all classes in the <a href="qtcore.html#qtcore">QtCore</a> and <a href="qtgui.html#qtgui">QtGui</a> libraries. This saves us from the trouble of having to include every class individually. We also include <tt>mainwindow.h</tt>.</p>
92
<p>You might wonder why we don't include <tt><QtGui></tt> in <tt>mainwindow.h</tt> and be done with it. The reason is that including such a large header from another header file can rapidly degrade performances. Here, it wouldn't do any harm, but it's still generally a good idea to include only the header files that are strictly necessary from another header file.</p>
93
<pre> MainWindow::MainWindow()
95
textEdit = new QTextEdit;
96
setCentralWidget(textEdit);
105
connect(textEdit->document(), SIGNAL(contentsChanged()),
106
this, SLOT(documentWasModified()));
108
setWindowTitle(tr("Application"));
110
<p>In the constructor, we start by creating a <a href="qtextedit.html">QTextEdit</a> widget as a child of the main window (the <tt>this</tt> object). Then we call <a href="qmainwindow.html#setCentralWidget">QMainWindow::setCentralWidget</a>() to tell that this is going to be the widget that occupies the central area of the main window, between the toolbars and the status bar.</p>
111
<p>Then we call <tt>createActions()</tt>, <tt>createMenus()</tt>, <tt>createToolBars()</tt>, and <tt>createStatusBar()</tt>, four private functions that set up the user interface. After that, we call <tt>readSettings()</tt> to restore the user's preferences.</p>
112
<p>We establish a signal-slot connection between the <a href="qtextedit.html">QTextEdit</a>'s document object and our <tt>documentWasModified()</tt> slot. Whenever the user modifies the text in the <a href="qtextedit.html">QTextEdit</a>, we want to update the title bar to show that the file was modified.</p>
113
<p>At the end, we set the window title to "Application", the name of our program. The <a href="qobject.html#tr">tr()</a> call around the literal string marks the string for translation. It is a good habit to call <a href="qobject.html#tr">tr()</a> on all user-visible strings, in case you later decide to translate your application to other languages. The <a href="i18n.html">Internationalization with Qt</a> overview convers <a href="qobject.html#tr">tr()</a> in more detail.</p>
114
<a name="close-event-handler"></a><pre> void MainWindow::closeEvent(QCloseEvent *event)
123
<p>When the user attempts to close the window, we call the private function <tt>maybeSave()</tt> to give the user the possibility to save pending changes. The function returns true if the user wants the application to close; otherwise, it returns false. In the first case, we save the user's preferences to disk and accept the close event; in the second case, we ignore the close event, meaning that the application will stay up and running as if nothing happened.</p>
124
<pre> void MainWindow::newFile()
127
textEdit->clear();
128
setCurrentFile("");
131
<p>The <tt>newFile()</tt> slot is invoked when the user selects <b>File|New</b> from the menu. We call <tt>maybeSave()</tt> to save any pending changes and if the user accepts to go on, we clear the <a href="qtextedit.html">QTextEdit</a> and call the private function <tt>setCurrentFile()</tt> to update the window title and clear the <a href="qwidget.html#windowModified-prop">windowModified</a> flag.</p>
132
<pre> void MainWindow::open()
135
QString fileName = QFileDialog::getOpenFileName(this);
136
if (!fileName.isEmpty())
140
<p>The <tt>open()</tt> slot is invoked when the user clicks <b>File|Open</b>. We pop up a <a href="qfiledialog.html">QFileDialog</a> asking the user to choose a file. If the user chooses a file (i.e., <tt>fileName</tt> is not an empty string), we call the private function <tt>loadFile()</tt> to actually load the file.</p>
141
<pre> bool MainWindow::save()
143
if (curFile.isEmpty()) {
146
return saveFile(curFile);
149
<p>The <tt>save()</tt> slot is invoked when the user clicks <b>File|Save</b>. If the user hasn't provided a name for the file yet, we call <tt>saveAs()</tt>; otherwise, we call the private function <tt>saveFile()</tt> to actually save the file.</p>
150
<pre> bool MainWindow::saveAs()
152
QString fileName = QFileDialog::getSaveFileName(this);
153
if (fileName.isEmpty())
156
return saveFile(fileName);
158
<p>In <tt>saveAs()</tt>, we start by popping up a <a href="qfiledialog.html">QFileDialog</a> asking the user to provide a name. If the user clicks <b>Cancel</b>, the returned file name is empty, and we do nothing.</p>
159
<pre> void MainWindow::about()
161
QMessageBox::about(this, tr("About Application"),
162
tr("The <b>Application</b> example demonstrates how to "
163
"write modern GUI applications using Qt, with a menu bar, "
164
"toolbars, and a status bar."));
166
<p>The application's About box is done using one statement, using the <a href="qmessagebox.html#about">QMessageBox::about</a>() static function and relying on its support for an HTML subset.</p>
167
<pre> void MainWindow::documentWasModified()
169
setWindowModified(true);
171
<p>The <tt>documentWasModified()</tt> slot is invoked each time the text in the <a href="qtextedit.html">QTextEdit</a> changes because of user edits. We call <a href="qwidget.html#windowModified-prop">QWidget::setWindowModified</a>() to make the title bar show that the file was modified. How this is done depends varies on the different platforms.</p>
172
<pre> void MainWindow::createActions()
174
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
175
newAct->setShortcut(tr("Ctrl+N"));
176
newAct->setStatusTip(tr("Create a new file"));
177
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
179
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
180
openAct->setShortcut(tr("Ctrl+O"));
181
openAct->setStatusTip(tr("Open an existing file"));
182
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
184
aboutQtAct = new QAction(tr("About &Qt"), this);
185
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
186
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
188
<p>The <tt>createActions()</tt> private function, which is called from the <tt>MainWindow</tt> constructor, creates <a href="qaction.html">QAction</a>s. The code is very repetitive, so we show only the actions corresponding to <b>File|New</b>, <b>File|Open</b>, and <b>Help|About Qt</b>.</p>
189
<p>A <a href="qaction.html">QAction</a> is an object that represents one user action, such as saving a file or invoking a dialog. An action can be put in a <a href="qmenu.html">QMenu</a> or a <a href="qtoolbar.html">QToolBar</a>, or both, or in any other widget that reimplements <a href="qwidget.html#actionEvent">QWidget::actionEvent</a>().</p>
190
<p>An action has a text that is shown in the menu, an icon, a shortcut key, a tooltip, a status tip (shown in the status bar), a "What's This?" text, and more. It emits a <a href="qaction.html#triggered">triggered()</a> signal whenever the user invokes the action (e.g., by clicking the associated menu item or toolbar button). We connect this signal to a slot that performs the actual action.</p>
191
<p>The code above contains one more idiom that must be explained. For some of the actions, we specify an icon as a <a href="qpixmap.html">QPixmap</a> to the <a href="qaction.html">QAction</a> constructor. The <a href="qpixmap.html">QPixmap</a> constructor takes the file name of an image that it tries to load. Here, the file name starts with <tt>:</tt>. Such file names aren't ordinary file names, but rather path in the executable's stored resources. We'll come back to this when we review the <tt>application.qrc</tt> file that's part of the project.</p>
192
<pre> void MainWindow::createMenus()
194
fileMenu = menuBar()->addMenu(tr("&File"));
195
fileMenu->addAction(newAct);
196
fileMenu->addAction(openAct);
197
fileMenu->addAction(saveAct);
198
fileMenu->addAction(saveAsAct);
199
fileMenu->addSeparator();
200
fileMenu->addAction(exitAct);
202
editMenu = menuBar()->addMenu(tr("&Edit"));
203
editMenu->addAction(cutAct);
204
editMenu->addAction(copyAct);
205
editMenu->addAction(pasteAct);
207
menuBar()->addSeparator();
209
helpMenu = menuBar()->addMenu(tr("&Help"));
210
helpMenu->addAction(aboutAct);
211
helpMenu->addAction(aboutQtAct);
213
<p>Creating actions isn't sufficient to make them available to the user; we must also add them to the menu system. This is what <tt>createMenus()</tt> does. We create a <b>File</b>, an <b>Edit</b>, and a <b>Help</b> menu. <a href="qmainwindow.html#menuBar">QMainWindow::menuBar</a>() lets us access the window's menu bar widget. We don't have to worry about creating the menu bar ourselves; the first time we call this function, the <a href="qmenubar.html">QMenuBar</a> is created.</p>
214
<p>Just before we create the <b>Help</b> menu, we call <a href="qmenubar.html#addSeparator">QMenuBar::addSeparator</a>(). This has no effect for most widget styles (e.g., Windows and Mac OS X styles), but for Motif-based styles this makes sure that <b>Help</b> is pushed to the right side of the menu bar. Try running the application with various styles and see the result:</p>
215
<pre> application -style=windows
216
application -style=motif
217
application -style=cde</pre>
218
<p>Let's now review the toolbars:</p>
219
<pre> void MainWindow::createToolBars()
221
fileToolBar = addToolBar(tr("File"));
222
fileToolBar->addAction(newAct);
223
fileToolBar->addAction(openAct);
224
fileToolBar->addAction(saveAct);
226
editToolBar = addToolBar(tr("Edit"));
227
editToolBar->addAction(cutAct);
228
editToolBar->addAction(copyAct);
229
editToolBar->addAction(pasteAct);
231
<p>Creating toolbars is very similar to creating menus. The same actions that we put in the menus can be reused in the toolbars.</p>
232
<pre> void MainWindow::createStatusBar()
234
statusBar()->showMessage(tr("Ready"));
236
<p><a href="qmainwindow.html#statusBar">QMainWindow::statusBar</a>() returns a pointer to the main window's <a href="qstatusbar.html">QStatusBar</a> widget. Like with <a href="qmainwindow.html#menuBar">QMainWindow::menuBar</a>(), the widget is automatically created the first time the function is called.</p>
237
<pre> void MainWindow::readSettings()
239
QSettings settings("Trolltech", "Application Example");
240
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
241
QSize size = settings.value("size", QSize(400, 400)).toSize();
245
<p>The <tt>readSettings()</tt> function is called from the constructor to load the user's preferences and other application settings. The <a href="qsettings.html">QSettings</a> class provides a high-level interface for storing settings permanently on disk. On Windows, it uses the (in)famous Windows registry; on Mac OS X, it uses the native XML-based CFPreferences API; on Unix/<a href="winsystem.html#x11">X11</a>, it uses text files.</p>
246
<p>The <a href="qsettings.html">QSettings</a> constructor takes arguments that identify your company and the name of the product. This ensures that the settings for different applications are kept separately.</p>
247
<p>We use <a href="qsettings.html#value">QSettings::value</a>() to extract the value of the "pos" and "size" settings. The second argument to <a href="qsettings.html#value">QSettings::value</a>() is optional and specifies a default value for the setting if there exists none. This value is used the first time the application is run.</p>
248
<p>When restoring the position and size of a window, it's important to call <a href="qwidget.html#size-prop">QWidget::resize</a>() before <a href="qwidget.html#pos-prop">QWidget::move</a>(). The reason why is given in the <a href="geometry.html">Window Geometry</a> overview.</p>
249
<pre> void MainWindow::writeSettings()
251
QSettings settings("Trolltech", "Application Example");
252
settings.setValue("pos", pos());
253
settings.setValue("size", size());
255
<p>The <tt>writeSettings()</tt> function is called from <tt>closeEvent()</tt>. Writing settings is similar to reading them, except simpler. The arguments to the <a href="qsettings.html">QSettings</a> constructor must be the same as in <tt>readSettings()</tt>.</p>
256
<pre> bool MainWindow::maybeSave()
258
if (textEdit->document()->isModified()) {
259
int ret = QMessageBox::warning(this, tr("Application"),
260
tr("The document has been modified.\n"
261
"Do you want to save your changes?"),
262
QMessageBox::Yes | QMessageBox::Default,
264
QMessageBox::Cancel | QMessageBox::Escape);
265
if (ret == QMessageBox::Yes)
267
else if (ret == QMessageBox::Cancel)
272
<p>The <tt>maybeSave()</tt> function is called to save pending changes. If there are pending changes, it pops up a <a href="qmessagebox.html">QMessageBox</a> giving the user to save the document. The options are <a href="qmessagebox.html#Button-enum">QMessageBox::Yes</a>, <a href="qmessagebox.html#Button-enum">QMessageBox::No</a>, and <a href="qmessagebox.html#Button-enum">QMessageBox::Cancel</a>. The <b>Yes</b> button is made the default button (the button that is invoked when the user presses <b>Return</b>) using the <a href="qmessagebox.html#Button-enum">QMessageBox::Default</a> flag; the <b>Cancel</b> button is made the escape button (the button that is invoked when the user presses <b>Esc</b>) using the <a href="qmessagebox.html#Button-enum">QMessageBox::Escape</a> flag.</p>
273
<p>The <tt>maybeSave()</tt> function returns <tt>true</tt> in all cases, except when the user clicks <b>Cancel</b>. The caller must check the return value and stop whatever it was doing if the return value is <tt>false</tt>.</p>
274
<pre> void MainWindow::loadFile(const QString &fileName)
276
QFile file(fileName);
277
if (!file.open(QFile::ReadOnly | QFile::Text)) {
278
QMessageBox::warning(this, tr("Application"),
279
tr("Cannot read file %1:\n%2.")
281
.arg(file.errorString()));
285
QTextStream in(&file);
286
QApplication::setOverrideCursor(Qt::WaitCursor);
287
textEdit->setPlainText(in.readAll());
288
QApplication::restoreOverrideCursor();
290
setCurrentFile(fileName);
291
statusBar()->showMessage(tr("File loaded"), 2000);
293
<p>In <tt>loadFile()</tt>, we use <a href="qfile.html">QFile</a> and <a href="qtextstream.html">QTextStream</a> to read in the data. The <a href="qfile.html">QFile</a> object provides access to the bytes stored in a file.</p>
294
<p>We start by opening the file in read-only mode. The <a href="qiodevice.html#OpenModeFlag-enum">QFile::Text</a> flag indicates that the file is a text file, not a binary file. On Unix and Mac OS X, this makes no difference, but on Windows, it ensures that the "\r\n" end-of-line sequence is converted to "\n" when reading.</p>
295
<p>If we successfully opened the file, we use a <a href="qtextstream.html">QTextStream</a> object to read in the data. <a href="qtextstream.html">QTextStream</a> automatically converts the 8-bit data into a Unicode <a href="qstring.html">QString</a> and supports various encodings. If no encoding is specified, <a href="qtextstream.html">QTextStream</a> assumes the file is written using the system's default 8-bit encoding (for example, Latin-1; see <a href="qtextcodec.html#codecForLocale">QTextCodec::codecForLocale</a>() for details).</p>
296
<p>Since the call to <a href="qtextstream.html#readAll">QTextStream::readAll</a>() might take some time, we set the cursor to be <a href="qt.html#CursorShape-enum">Qt::WaitCursor</a> for the entire application while it goes on.</p>
297
<p>At the end, we call the private <tt>setCurrentFile()</tt> function, which we'll cover in a moment, and we display the string "File loaded" in the status bar for 2 seconds (2000 milliseconds).</p>
298
<pre> bool MainWindow::saveFile(const QString &fileName)
300
QFile file(fileName);
301
if (!file.open(QFile::WriteOnly | QFile::Text)) {
302
QMessageBox::warning(this, tr("Application"),
303
tr("Cannot write file %1:\n%2.")
305
.arg(file.errorString()));
309
QTextStream out(&file);
310
QApplication::setOverrideCursor(Qt::WaitCursor);
311
out << textEdit->toPlainText();
312
QApplication::restoreOverrideCursor();
314
setCurrentFile(fileName);
315
statusBar()->showMessage(tr("File saved"), 2000);
318
<p>Saving a file is very similar to loading one. Here, the <a href="qiodevice.html#OpenModeFlag-enum">QFile::Text</a> flag ensures that on Windows, "\n" is converted into "\r\n" to conform to the Windows convension.</p>
319
<pre> void MainWindow::setCurrentFile(const QString &fileName)
322
textEdit->document()->setModified(false);
323
setWindowModified(false);
325
if (curFile.isEmpty())
326
setWindowTitle(tr("Application"));
328
setWindowTitle(tr("%1[*] - %2").arg(strippedName(curFile))
329
.arg(tr("Application")));
331
<p>The <tt>setCurrentFile()</tt> function is called to reset the state of a few variables when a file is loaded or saved, or when the user starts editing a new file (in which case <tt>fileName</tt> is empty). We update the <tt>curFile</tt> variable, clear the <a href="qtextdocument.html#modified-prop">QTextDocument::modified</a> flag and the associated <tt>QWidget:windowModified</tt> flag, and update the window title to contain the new file name.</p>
332
<p>The <tt>strippedName()</tt> function call around <tt>curFile</tt> in the <a href="qwidget.html#windowTitle-prop">QWidget::setWindowTitle</a>() call shortens the file name to exclude the path. Here's the function:</p>
333
<pre> QString MainWindow::strippedName(const QString &fullFileName)
335
return QFileInfo(fullFileName).fileName();
337
<a name="the-main-function"></a>
338
<h2>The main() Function</h2>
339
<p>The <tt>main()</tt> function for this application is typical of applications that contain one main window:</p>
340
<pre> #include <QApplication>
342
#include "mainwindow.h"
344
int main(int argc, char *argv[])
346
Q_INIT_RESOURCE(application);
348
QApplication app(argc, argv);
353
<a name="the-resource-file"></a>
354
<h2>The Resource File</h2>
355
<p>As you will probably recall, for some of the actions, we specified icons with file names starting with <tt>:</tt> and mentioned that such file names aren't ordinary file names, but path in the executable's stored resources. These resources are compiled</p>
356
<p>The resources associated with an application are specified in a <tt>.qrc</tt> file, an XML-based file format that lists files on the disk. Here's the <tt>application.qrc</tt> file that's used by the Application example:</p>
357
<pre> <!DOCTYPE RCC><RCC version="1.0">
359
<file>images/copy.png</file>
360
<file>images/cut.png</file>
361
<file>images/new.png</file>
362
<file>images/open.png</file>
363
<file>images/paste.png</file>
364
<file>images/save.png</file>
367
<p>The <tt>.png</tt> files listed in the <tt>application.qrc</tt> file are files that are part of the Application example's source tree. Paths are relative to the directory where the <tt>application.qrc</tt> file is located (the <tt>mainwindows/application</tt> directory).</p>
368
<p>The resource file must be mentioned in the <tt>application.pro</tt> file so that <tt>qmake</tt> knows about it:</p>
369
<pre> RESOURCES = application.qrc</pre>
370
<p><tt>qmake</tt> will produce make rules to generate a file called <tt>qrc_application.cpp</tt> that is linked into the application. This file contains all the data for the images and other resources as static C++ arrays of compressed binary data. See <a href="resources.html">The Qt Resource System</a> for more information about resources.</p>
371
<p /><address><hr /><div align="center">
372
<table width="100%" cellspacing="0" border="0"><tr class="address">
373
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
374
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
375
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
376
</tr></table></div></address></body>