Commit | Line | Data |
---|---|---|
2340bcd3 JVH |
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 | <!-- 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"> </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) 2010 Nokia Corporation and/or its subsidiary(-ies). | |
32 | ** All rights reserved. | |
33 | ** | |
34 | ** Contact: Nokia Corporation (qt-info@nokia.com) | |
35 | ** | |
36 | ** This file is part of a Qt Solutions component. | |
37 | ** | |
38 | ** You may use this file under the terms of the BSD license as follows: | |
39 | ** | |
40 | ** "Redistribution and use in source and binary forms, with or without | |
41 | ** modification, are permitted provided that the following conditions are | |
42 | ** met: | |
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 | |
48 | ** distribution. | |
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 | |
52 | ** permission. | |
53 | ** | |
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." | |
65 | ** | |
66 | ****************************************************************************/</span> | |
67 | ||
68 | #include "qtsinglecoreapplication.h" | |
69 | #include <QtCore/QDebug> | |
70 | ||
71 | void report(const QString& msg) | |
72 | { | |
73 | qDebug("[%i] %s", (int)QCoreApplication::applicationPid(), qPrintable(msg)); | |
74 | } | |
75 | ||
76 | class MainClass : public QObject | |
77 | { | |
78 | Q_OBJECT | |
79 | public: | |
80 | MainClass() | |
81 | : QObject() | |
82 | {} | |
83 | ||
84 | public slots: | |
85 | void handleMessage(const QString& message) | |
86 | { | |
87 | report( "Message received: \"" + message + "\""); | |
88 | } | |
89 | }; | |
90 | ||
91 | int main(int argc, char **argv) | |
92 | { | |
93 | report("Starting up"); | |
94 | ||
95 | QtSingleCoreApplication app(argc, argv); | |
96 | ||
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."; | |
102 | report(rep); | |
103 | return 0; | |
104 | } else { | |
105 | report("No other instance is running; so I will."); | |
106 | MainClass mainObj; | |
107 | QObject::connect(&app, SIGNAL(messageReceived(const QString&)), | |
108 | &mainObj, SLOT(handleMessage(const QString&))); | |
109 | return app.exec(); | |
110 | } | |
111 | } | |
112 | ||
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> | |
120 | </html> |