Debian Codegraph @

thirdparty/singleapp/doc/html/qtsingleapplication-example-loader.html

raw
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">&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) 2013 Digia Plc and/or its subsidiary(-ies).
21 ** Contact: http://www.qt-project.org/legal
22 **
23 ** This file is part of the Qt Solutions component.
24 **
25 ** You may use this file under the terms of the BSD license as follows:
26 **
27 ** &quot;Redistribution and use in source and binary forms, with or without
28 ** modification, are permitted provided that the following conditions are
29 ** met:
30 ** * Redistributions of source code must retain the above copyright
31 ** notice, this list of conditions and the following disclaimer.
32 ** * Redistributions in binary form must reproduce the above copyright
33 ** notice, this list of conditions and the following disclaimer in
34 ** the documentation and/or other materials provided with the
35 ** distribution.
36 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
37 ** the names of its contributors may be used to endorse or promote
38 ** products derived from this software without specific prior written
39 ** permission.
40 **
41 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42 ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
45 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
52 **
53 ****************************************************************************/</span>
54
55 #include &lt;qtsingleapplication.h&gt;
56 #include &lt;QtCore/QFile&gt;
57 #include &lt;QtGui/QMainWindow&gt;
58 #include &lt;QtGui/QPrinter&gt;
59 #include &lt;QtGui/QPainter&gt;
60 #include &lt;QtGui/QTextEdit&gt;
61 #include &lt;QtGui/QMdiArea&gt;
62 #include &lt;QtCore/QTextStream&gt;
63
64 class MainWindow : public QMainWindow
65 {
66 Q_OBJECT
67 public:
68 MainWindow();
69
70 public slots:
71 void handleMessage(const QString&amp; message);
72
73 signals:
74 void needToShow();
75
76 private:
77 QMdiArea *workspace;
78 };</pre>
79 <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>
80 <pre> MainWindow::MainWindow()
81 {
82 workspace = new QMdiArea(this);
83
84 setCentralWidget(workspace);
85 }</pre>
86 <p>The <a href="http://qt.nokia.com/doc/4.6/designer-to-know.html">MainWindow</a> constructor creates a minimal user interface.</p>
87 <pre> void MainWindow::handleMessage(const QString&amp; message)
88 {
89 enum Action {
90 Nothing,
91 Open,
92 Print
93 } action;
94
95 action = Nothing;
96 QString filename = message;
97 if (message.toLower().startsWith(&quot;/print &quot;)) {
98 filename = filename.mid(7);
99 action = Print;
100 } else if (!message.isEmpty()) {
101 action = Open;
102 }
103 if (action == Nothing) {
104 emit needToShow();
105 return;
106 }
107
108 QFile file(filename);
109 QString contents;
110 if (file.open(QIODevice::ReadOnly))
111 contents = file.readAll();
112 else
113 contents = &quot;[[Error: Could not load file &quot; + filename + &quot;]]&quot;;
114
115 QTextEdit *view = new QTextEdit;
116 view-&gt;setPlainText(contents);
117
118 switch(action) {</pre>
119 <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>
120 <pre> case Print:
121 {
122 QPrinter printer;
123 view-&gt;print(&amp;printer);
124 delete view;
125 }
126 break;
127
128 case Open:
129 {
130 workspace-&gt;addSubWindow(view);
131 view-&gt;setWindowTitle(message);
132 view-&gt;show();
133 emit needToShow();
134 }
135 break;
136 default:
137 break;
138 };
139 }</pre>
140 <p>Loading the file will also activate the window.</p>
141 <pre> #include &quot;main.moc&quot;
142
143 int main(int argc, char **argv)
144 {
145 QtSingleApplication instance(&quot;File loader QtSingleApplication example&quot;, argc, argv);
146 QString message;
147 for (int a = 1; a &lt; argc; ++a) {
148 message += argv[a];
149 if (a &lt; argc-1)
150 message += &quot; &quot;;
151 }
152
153 if (instance.sendMessage(message))
154 return 0;</pre>
155 <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>
156 <pre> MainWindow mw;
157 mw.handleMessage(message);
158 mw.show();
159
160 QObject::connect(&amp;instance, SIGNAL(messageReceived(const QString&amp;)),
161 &amp;mw, SLOT(handleMessage(const QString&amp;)));
162
163 instance.setActivationWindow(&amp;mw, false);
164 QObject::connect(&amp;mw, SIGNAL(needToShow()), &amp;instance, SLOT(activateWindow()));
165
166 return instance.exec();
167 }</pre>
168 <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>
169 <p /><address><hr /><div align="center">
170 <table width="100%" cellspacing="0" border="0"><tr class="address">
171 <td width="30%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
172 <td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td>
173 <td width="30%" align="right"><div align="right">Qt Solutions</div></td>
174 </tr></table></div></address></body>
175 </html>