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>A non-GUI example
</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">A non-GUI example
<br /><span class=
"subtitle"></span>
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>
26 include(../../src/qtsinglecoreapplication.pri)
29 <pre><span class=
"comment"> /****************************************************************************
31 ** Copyright (C)
2010 Nokia Corporation and/or its subsidiary(-ies).
32 ** All rights reserved.
34 ** Contact: Nokia Corporation (qt-info@nokia.com)
36 ** This file is part of a Qt Solutions component.
38 ** You may use this file under the terms of the BSD license as follows:
40 **
"Redistribution and use in source and binary forms, with or without
41 ** modification, are permitted provided that the following conditions are
43 ** * Redistributions of source code must retain the above copyright
44 ** notice, this list of conditions and the following disclaimer.
45 ** * Redistributions in binary form must reproduce the above copyright
46 ** notice, this list of conditions and the following disclaimer in
47 ** the documentation and/or other materials provided with the
49 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
50 ** the names of its contributors may be used to endorse or promote
51 ** products derived from this software without specific prior written
54 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 **
"AS IS
" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"
66 ****************************************************************************/
</span>
68 #include
"qtsinglecoreapplication.h
"
69 #include
<QtCore/QDebug
>
71 void report(const QString
& msg)
73 qDebug(
"[%i] %s
", (int)QCoreApplication::applicationPid(), qPrintable(msg));
76 class MainClass : public QObject
85 void handleMessage(const QString
& message)
87 report(
"Message received: \
"" + message +
"\
"");
91 int main(int argc, char **argv)
93 report(
"Starting up
");
95 QtSingleCoreApplication app(argc, argv);
97 if (app.isRunning()) {
98 QString msg(QString(
"Hi master, I am %
1.
").arg(QCoreApplication::applicationPid()));
99 bool sentok = app.sendMessage(msg,
2000);
100 QString rep(
"Another instance is running, so I will exit.
");
101 rep += sentok ?
" Message sent ok.
" :
" Message sending failed; the other instance may be frozen.
";
105 report(
"No other instance is running; so I will.
");
107 QObject::connect(
&app, SIGNAL(messageReceived(const QString
&)),
108 &mainObj, SLOT(handleMessage(const QString
&)));
113 #include
"main.moc
"</pre>
114 <p /><address><hr /><div align=
"center">
115 <table width=
"100%" cellspacing=
"0" border=
"0"><tr class=
"address">
116 <td width=
"30%" align=
"left">Copyright
© 2010 Nokia Corporation and/or its subsidiary(-ies)
</td>
117 <td width=
"40%" align=
"center"><a href=
"http://qt.nokia.com/doc/trademarks.html">Trademarks
</a></td>
118 <td width=
"30%" align=
"right"><div align=
"right">Qt Solutions
</div></td>
119 </tr></table></div></address></body>