Commit | Line | Data |
---|---|---|
15c988f7 JB |
1 | <?xml version="1.0" encoding="iso-8859-1"?> |
2 | <!DOCTYPE html | |
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 | <!-- loader.qdoc --> | |
6 | <head> | |
7 | <title>Loading Documents</title> | |
8 | <link href="classic.css" rel="stylesheet" type="text/css" /> | |
9 | </head> | |
10 | <body> | |
11 | <table border="0" cellpadding="0" cellspacing="0" width="100%"> | |
12 | <tr> | |
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> | |
16 | </h1> | |
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"> /**************************************************************************** | |
19 | ** | |
20 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). | |
21 | ** All rights reserved. | |
22 | ** | |
23 | ** Contact: Nokia Corporation (qt-info@nokia.com) | |
24 | ** | |
25 | ** This file is part of a Qt Solutions component. | |
26 | ** | |
27 | ** You may use this file under the terms of the BSD license as follows: | |
28 | ** | |
29 | ** "Redistribution and use in source and binary forms, with or without | |
30 | ** modification, are permitted provided that the following conditions are | |
31 | ** met: | |
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 | |
37 | ** distribution. | |
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 | |
41 | ** permission. | |
42 | ** | |
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." | |
54 | ** | |
55 | ****************************************************************************/</span> | |
56 | ||
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> | |
65 | ||
66 | class MainWindow : public QMainWindow | |
67 | { | |
68 | Q_OBJECT | |
69 | public: | |
70 | MainWindow(); | |
71 | ||
72 | public slots: | |
73 | void handleMessage(const QString& message); | |
74 | ||
75 | signals: | |
76 | void needToShow(); | |
77 | ||
78 | private: | |
79 | QMdiArea *workspace; | |
80 | };</pre> | |
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() | |
83 | { | |
84 | workspace = new QMdiArea(this); | |
85 | ||
86 | setCentralWidget(workspace); | |
87 | }</pre> | |
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) | |
90 | { | |
91 | enum Action { | |
92 | Nothing, | |
93 | Open, | |
94 | ||
95 | } action; | |
96 | ||
97 | action = Nothing; | |
98 | QString filename = message; | |
99 | if (message.toLower().startsWith("/print ")) { | |
100 | filename = filename.mid(7); | |
101 | action = Print; | |
102 | } else if (!message.isEmpty()) { | |
103 | action = Open; | |
104 | } | |
105 | if (action == Nothing) { | |
106 | emit needToShow(); | |
107 | return; | |
108 | } | |
109 | ||
110 | QFile file(filename); | |
111 | QString contents; | |
112 | if (file.open(QIODevice::ReadOnly)) | |
113 | contents = file.readAll(); | |
114 | else | |
115 | contents = "[[Error: Could not load file " + filename + "]]"; | |
116 | ||
117 | QTextEdit *view = new QTextEdit; | |
118 | view->setPlainText(contents); | |
119 | ||
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> | |
122 | <pre> case Print: | |
123 | { | |
124 | QPrinter printer; | |
125 | view->print(&printer); | |
126 | delete view; | |
127 | } | |
128 | break; | |
129 | ||
130 | case Open: | |
131 | { | |
132 | workspace->addSubWindow(view); | |
133 | view->setWindowTitle(message); | |
134 | view->show(); | |
135 | emit needToShow(); | |
136 | } | |
137 | break; | |
138 | default: | |
139 | break; | |
140 | }; | |
141 | }</pre> | |
142 | <p>Loading the file will also activate the window.</p> | |
143 | <pre> #include "main.moc" | |
144 | ||
145 | int main(int argc, char **argv) | |
146 | { | |
147 | QtSingleApplication instance("File loader QtSingleApplication example", argc, argv); | |
148 | QString message; | |
149 | for (int a = 1; a < argc; ++a) { | |
150 | message += argv[a]; | |
151 | if (a < argc-1) | |
152 | message += " "; | |
153 | } | |
154 | ||
155 | if (instance.sendMessage(message)) | |
156 | return 0;</pre> | |
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> | |
158 | <pre> MainWindow mw; | |
159 | mw.handleMessage(message); | |
160 | mw.show(); | |
161 | ||
162 | QObject::connect(&instance, SIGNAL(messageReceived(const QString&)), | |
163 | &mw, SLOT(handleMessage(const QString&))); | |
164 | ||
165 | instance.setActivationWindow(&mw, false); | |
166 | QObject::connect(&mw, SIGNAL(needToShow()), &instance, SLOT(activateWindow())); | |
167 | ||
168 | return instance.exec(); | |
169 | }</pre> | |
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> | |
177 | </html> |