Debian Codegraph @

3rdparty/qtsingleapplication-2.6_1-opensource/doc/html/qtsingleapplication-example-loader.html

raw symbols SCIP
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 <!-- qtsingleapplication-2.6_1-opensource/examples/loader/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">&nbsp;&nbsp;</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) 2009 Nokia Corporation and/or its subsidiary(-ies).
21 ** All rights reserved.
22 ** Contact: Nokia Corporation ([email protected])
23 **
24 ** This file is part of a Qt Solutions component.
25 **
26 ** Commercial Usage
27 ** Licensees holding valid Qt Commercial licenses may use this file in
28 ** accordance with the Qt Solutions Commercial License Agreement provided
29 ** with the Software or, alternatively, in accordance with the terms
30 ** contained in a written agreement between you and Nokia.
31 **
32 ** GNU Lesser General Public License Usage
33 ** Alternatively, this file may be used under the terms of the GNU Lesser
34 ** General Public License version 2.1 as published by the Free Software
35 ** Foundation and appearing in the file LICENSE.LGPL included in the
36 ** packaging of this file. Please review the following information to
37 ** ensure the GNU Lesser General Public License version 2.1 requirements
38 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
39 **
40 ** In addition, as a special exception, Nokia gives you certain
41 ** additional rights. These rights are described in the Nokia Qt LGPL
42 ** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
43 ** package.
44 **
45 ** GNU General Public License Usage
46 ** Alternatively, this file may be used under the terms of the GNU
47 ** General Public License version 3.0 as published by the Free Software
48 ** Foundation and appearing in the file LICENSE.GPL included in the
49 ** packaging of this file. Please review the following information to
50 ** ensure the GNU General Public License version 3.0 requirements will be
51 ** met: http://www.gnu.org/copyleft/gpl.html.
52 **
53 ** Please note Third Party Software included with Qt Solutions may impose
54 ** additional restrictions and it is the user's responsibility to ensure
55 ** that they have met the licensing requirements of the GPL, LGPL, or Qt
56 ** Solutions Commercial license and the relevant license of the Third
57 ** Party Software they are using.
58 **
59 ** If you are unsure which license is appropriate for your use, please
60 ** contact Nokia at [email protected].
61 **
62 ****************************************************************************/</span>
63
64 #include &lt;qtsingleapplication.h&gt;
65 #include &lt;QtCore/QFile&gt;
66 #include &lt;QtGui/QMainWindow&gt;
67 #include &lt;QtGui/QPrinter&gt;
68 #include &lt;QtGui/QPainter&gt;
69 #include &lt;QtGui/QTextEdit&gt;
70 #include &lt;QtGui/QMdiArea&gt;
71 #include &lt;QtCore/QTextStream&gt;
72
73 class MainWindow : public QMainWindow
74 {
75 Q_OBJECT
76 public:
77 MainWindow();
78
79 public slots:
80 void handleMessage(const QString&amp; message);
81
82 signals:
83 void needToShow();
84
85 private:
86 QMdiArea *workspace;
87 };</pre>
88 <p>The user interface in this application is a <a href="http://qt.nokia.com/doc/4.5/qmainwindow.html">QMainWindow</a> subclass with a <a href="http://qt.nokia.com/doc/4.5/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>
89 <pre> MainWindow::MainWindow()
90 {
91 workspace = new QMdiArea(this);
92
93 setCentralWidget(workspace);
94 }</pre>
95 <p>The <a href="http://qt.nokia.com/doc/4.5/designer-to-know.html">MainWindow</a> constructor creates a minimal user interface.</p>
96 <pre> void MainWindow::handleMessage(const QString&amp; message)
97 {
98 enum Action {
99 Nothing,
100 Open,
101 Print
102 } action;
103
104 action = Nothing;
105 QString filename = message;
106 if (message.toLower().startsWith(&quot;/print &quot;)) {
107 filename = filename.mid(7);
108 action = Print;
109 } else if (!message.isEmpty()) {
110 action = Open;
111 }
112 if (action == Nothing) {
113 emit needToShow();
114 return;
115 }
116
117 QFile file(filename);
118 QString contents;
119 if (file.open(QIODevice::ReadOnly))
120 contents = file.readAll();
121 else
122 contents = &quot;[[Error: Could not load file &quot; + filename + &quot;]]&quot;;
123
124 QTextEdit *view = new QTextEdit;
125 view-&gt;setPlainText(contents);
126
127 switch(action) {</pre>
128 <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>
129 <pre> case Print:
130 {
131 QPrinter printer;
132 view-&gt;print(&amp;printer);
133 delete view;
134 }
135 break;
136
137 case Open:
138 {
139 workspace-&gt;addSubWindow(view);
140 view-&gt;setWindowTitle(message);
141 view-&gt;show();
142 emit needToShow();
143 }
144 break;
145 default:
146 break;
147 };
148 }</pre>
149 <p>Loading the file will also activate the window.</p>
150 <pre> #include &quot;main.moc&quot;
151
152 int main(int argc, char **argv)
153 {
154 QtSingleApplication instance(&quot;File loader QtSingleApplication example&quot;, argc, argv);
155 QString message;
156 for (int a = 1; a &lt; argc; ++a) {
157 message += argv[a];
158 if (a &lt; argc-1)
159 message += &quot; &quot;;
160 }
161
162 if (instance.sendMessage(message))
163 return 0;</pre>
164 <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>
165 <pre> MainWindow mw;
166 mw.handleMessage(message);
167 mw.show();
168
169 QObject::connect(&amp;instance, SIGNAL(messageReceived(const QString&amp;)),
170 &amp;mw, SLOT(handleMessage(const QString&amp;)));
171
172 instance.setActivationWindow(&amp;mw, false);
173 QObject::connect(&amp;mw, SIGNAL(needToShow()), &amp;instance, SLOT(activateWindow()));
174
175 return instance.exec();
176 }</pre>
177 <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&#x2e; 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>
178 <p /><address><hr /><div align="center">
179 <table width="100%" cellspacing="0" border="0"><tr class="address">
180 <td width="30%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
181 <td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td>
182 <td width="30%" align="right"><div align="right">Qt Solutions</div></td>
183 </tr></table></div></address></body>
184 </html>