Debian Codegraph @

3rdparty/qtsingleapplication-2.6_1-opensource/doc/html/qtsinglecoreapplication-example-console.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/console/console.qdoc -->
6 <head>
7 <title>A non-GUI example</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">A non-GUI example<br /><span class="subtitle"></span>
16 </h1>
17 <p>This example shows how to use the single-application functionality in a console application. It does not require the <tt>QtGui</tt> library at all.</p>
18 <p>The only differences from the GUI application usage demonstrated in the other examples are:</p>
19 <p>1) The <tt>.pro</tt> file should include <tt>qtsinglecoreapplication.pri</tt> instead of <tt>qtsingleapplication.pri</tt></p>
20 <p>2) The class name is <tt>QtSingleCoreApplication</tt> instead of <tt>QtSingleApplication</tt>.</p>
21 <p>3) No calls are made regarding window activation, for obvious reasons.</p>
22 <p>console.pro:</p>
23 <pre> TEMPLATE = app
24 CONFIG += console
25 SOURCES += main.cpp
26 include(../../src/qtsinglecoreapplication.pri)
27 QT -= gui</pre>
28 <p>main.cpp:</p>
29 <pre><span class="comment"> /****************************************************************************
30 **
31 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
32 ** All rights reserved.
33 ** Contact: Nokia Corporation ([email protected])
34 **
35 ** This file is part of a Qt Solutions component.
36 **
37 ** Commercial Usage
38 ** Licensees holding valid Qt Commercial licenses may use this file in
39 ** accordance with the Qt Solutions Commercial License Agreement provided
40 ** with the Software or, alternatively, in accordance with the terms
41 ** contained in a written agreement between you and Nokia.
42 **
43 ** GNU Lesser General Public License Usage
44 ** Alternatively, this file may be used under the terms of the GNU Lesser
45 ** General Public License version 2.1 as published by the Free Software
46 ** Foundation and appearing in the file LICENSE.LGPL included in the
47 ** packaging of this file. Please review the following information to
48 ** ensure the GNU Lesser General Public License version 2.1 requirements
49 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
50 **
51 ** In addition, as a special exception, Nokia gives you certain
52 ** additional rights. These rights are described in the Nokia Qt LGPL
53 ** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
54 ** package.
55 **
56 ** GNU General Public License Usage
57 ** Alternatively, this file may be used under the terms of the GNU
58 ** General Public License version 3.0 as published by the Free Software
59 ** Foundation and appearing in the file LICENSE.GPL included in the
60 ** packaging of this file. Please review the following information to
61 ** ensure the GNU General Public License version 3.0 requirements will be
62 ** met: http://www.gnu.org/copyleft/gpl.html.
63 **
64 ** Please note Third Party Software included with Qt Solutions may impose
65 ** additional restrictions and it is the user's responsibility to ensure
66 ** that they have met the licensing requirements of the GPL, LGPL, or Qt
67 ** Solutions Commercial license and the relevant license of the Third
68 ** Party Software they are using.
69 **
70 ** If you are unsure which license is appropriate for your use, please
71 ** contact Nokia at [email protected].
72 **
73 ****************************************************************************/</span>
74
75 #include &quot;qtsinglecoreapplication.h&quot;
76 #include &lt;QtCore/QDebug&gt;
77
78 void report(const QString&amp; msg)
79 {
80 qDebug(&quot;[%i] %s&quot;, (int)QCoreApplication::applicationPid(), qPrintable(msg));
81 }
82
83 class MainClass : public QObject
84 {
85 Q_OBJECT
86 public:
87 MainClass()
88 : QObject()
89 {}
90
91 public slots:
92 void handleMessage(const QString&amp; message)
93 {
94 report( &quot;Message received: \&quot;&quot; + message + &quot;\&quot;&quot;);
95 }
96 };
97
98 int main(int argc, char **argv)
99 {
100 report(&quot;Starting up&quot;);
101
102 QtSingleCoreApplication app(argc, argv);
103
104 if (app.isRunning()) {
105 QString msg(QString(&quot;Hi master, I am %1.&quot;).arg(QCoreApplication::applicationPid()));
106 bool sentok = app.sendMessage(msg);
107 QString rep(&quot;Another instance is running, so I will exit.&quot;);
108 rep += sentok ? &quot; Message sent ok.&quot; : &quot; Message sending failed.&quot;;
109 report(rep);
110 return 0;
111 } else {
112 report(&quot;No other instance is running; so I will.&quot;);
113 MainClass mainObj;
114 QObject::connect(&amp;app, SIGNAL(messageReceived(const QString&amp;)),
115 &amp;mainObj, SLOT(handleMessage(const QString&amp;)));
116 return app.exec();
117 }
118 }
119
120 #include &quot;main.moc&quot;</pre>
121 <p /><address><hr /><div align="center">
122 <table width="100%" cellspacing="0" border="0"><tr class="address">
123 <td width="30%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
124 <td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td>
125 <td width="30%" align="right"><div align="right">Qt Solutions</div></td>
126 </tr></table></div></address></body>
127 </html>