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