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">
7 <title>Loading Documents
</title>
8 <link href=
"classic.css" rel=
"stylesheet" type=
"text/css" />
11 <table border=
"0" cellpadding=
"0" cellspacing=
"0" width=
"100%">
13 <td align=
"left" valign=
"top" width=
"32"><img src=
"images/qt-logo.png" align=
"left" width=
"57" height=
"67" border=
"0" /></td>
14 <td width=
"1"> </td><td class=
"postheader" valign=
"center"><a href=
"index.html"><font color=
"#004faf">Home
</font></a></td>
15 </tr></table><h1 class=
"title">Loading Documents
<br /><span class=
"subtitle"></span>
17 <p>The application in this example loads or prints the documents passed as commandline parameters to further instances of this application.
</p>
18 <pre><span class=
"comment"> /****************************************************************************
20 ** Copyright (C)
2010 Nokia Corporation and/or its subsidiary(-ies).
21 ** All rights reserved.
23 ** Contact: Nokia Corporation (qt-info@nokia.com)
25 ** This file is part of a Qt Solutions component.
27 ** You may use this file under the terms of the BSD license as follows:
29 **
"Redistribution and use in source and binary forms, with or without
30 ** modification, are permitted provided that the following conditions are
32 ** * Redistributions of source code must retain the above copyright
33 ** notice, this list of conditions and the following disclaimer.
34 ** * Redistributions in binary form must reproduce the above copyright
35 ** notice, this list of conditions and the following disclaimer in
36 ** the documentation and/or other materials provided with the
38 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
39 ** the names of its contributors may be used to endorse or promote
40 ** products derived from this software without specific prior written
43 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 **
"AS IS
" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"
55 ****************************************************************************/
</span>
57 #include
<qtsingleapplication.h
>
58 #include
<QtCore/QFile
>
59 #include
<QtGui/QMainWindow
>
60 #include
<QtGui/QPrinter
>
61 #include
<QtGui/QPainter
>
62 #include
<QtGui/QTextEdit
>
63 #include
<QtGui/QMdiArea
>
64 #include
<QtCore/QTextStream
>
66 class MainWindow : public QMainWindow
73 void handleMessage(const QString
& message);
81 <p>The user interface in this application is a
<a href=
"http://qt.nokia.com/doc/4.6/qmainwindow.html">QMainWindow
</a> subclass with a
<a href=
"http://qt.nokia.com/doc/4.6/qmdiarea.html">QMdiArea
</a> as the central widget. It implements a slot
<tt>handleMessage()
</tt> that will be connected to the messageReceived() signal of the
<a href=
"qtsingleapplication.html">QtSingleApplication
</a> class.
</p>
82 <pre> MainWindow::MainWindow()
84 workspace = new QMdiArea(this);
86 setCentralWidget(workspace);
88 <p>The
<a href=
"http://qt.nokia.com/doc/4.6/designer-to-know.html">MainWindow
</a> constructor creates a minimal user interface.
</p>
89 <pre> void MainWindow::handleMessage(const QString
& message)
98 QString filename = message;
99 if (message.toLower().startsWith(
"/print
")) {
100 filename = filename.mid(
7);
102 } else if (!message.isEmpty()) {
105 if (action == Nothing) {
110 QFile file(filename);
112 if (file.open(QIODevice::ReadOnly))
113 contents = file.readAll();
115 contents =
"[[Error: Could not load file
" + filename +
"]]
";
117 QTextEdit *view = new QTextEdit;
118 view-
>setPlainText(contents);
120 switch(action) {
</pre>
121 <p>The handleMessage() slot interprets the message passed in as a filename that can be prepended with
<i>/print
</i> to indicate that the file should just be printed rather than loaded.
</p>
125 view-
>print(
&printer);
132 workspace-
>addSubWindow(view);
133 view-
>setWindowTitle(message);
142 <p>Loading the file will also activate the window.
</p>
143 <pre> #include
"main.moc
"
145 int main(int argc, char **argv)
147 QtSingleApplication instance(
"File loader QtSingleApplication example
", argc, argv);
149 for (int a =
1; a
< argc; ++a) {
152 message +=
" ";
155 if (instance.sendMessage(message))
157 <p>The
<tt>main
</tt> entry point function creates a
<a href=
"qtsingleapplication.html">QtSingleApplication
</a> object, and creates a message to send to a running instance of the application. If the message was sent successfully the process exits immediately.
</p>
159 mw.handleMessage(message);
162 QObject::connect(
&instance, SIGNAL(messageReceived(const QString
&)),
163 &mw, SLOT(handleMessage(const QString
&)));
165 instance.setActivationWindow(
&mw, false);
166 QObject::connect(
&mw, SIGNAL(needToShow()),
&instance, SLOT(activateWindow()));
168 return instance.exec();
170 <p>If the message could not be sent the application starts up. Note that
<tt>false
</tt> is passed to the call to setActivationWindow() to prevent automatic activation for every message received, e.g
. when the application should just print a file. Instead, the message handling function determines whether activation is requested, and signals that by emitting the needToShow() signal. This is then simply connected directly to
<a href=
"qtsingleapplication.html">QtSingleApplication
</a>'s activateWindow() slot.
</p>
171 <p /><address><hr /><div align=
"center">
172 <table width=
"100%" cellspacing=
"0" border=
"0"><tr class=
"address">
173 <td width=
"30%" align=
"left">Copyright
© 2010 Nokia Corporation and/or its subsidiary(-ies)
</td>
174 <td width=
"40%" align=
"center"><a href=
"http://qt.nokia.com/doc/trademarks.html">Trademarks
</a></td>
175 <td width=
"30%" align=
"right"><div align=
"right">Qt Solutions
</div></td>
176 </tr></table></div></address></body>