| 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 |
|
| 6 |
<head> |
| 7 |
<title>A Trivial 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 Trivial Example<br /><span class="subtitle"></span> |
| 16 |
</h1> |
| 17 |
<p>The application in this example has a log-view that displays messages sent by further instances of the same application.</p> |
| 18 |
<p>The example demonstrates the use of the <a href="qtsingleapplication.html">QtSingleApplication</a> class to detect and communicate with a running instance of the application using the sendMessage() API. The messageReceived() signal is used to display received messages in a <a href="http://qt.nokia.com/doc/4.5/qtextedit.html">QTextEdit</a> log.</p> |
| 19 |
<pre><span class="comment"> /**************************************************************************** |
| 20 |
** |
| 21 |
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
| 22 |
** All rights reserved. |
| 23 |
** Contact: Nokia Corporation ([email protected]) |
| 24 |
** |
| 25 |
** This file is part of a Qt Solutions component. |
| 26 |
** |
| 27 |
** Commercial Usage |
| 28 |
** Licensees holding valid Qt Commercial licenses may use this file in |
| 29 |
** accordance with the Qt Solutions Commercial License Agreement provided |
| 30 |
** with the Software or, alternatively, in accordance with the terms |
| 31 |
** contained in a written agreement between you and Nokia. |
| 32 |
** |
| 33 |
** GNU Lesser General Public License Usage |
| 34 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
| 35 |
** General Public License version 2.1 as published by the Free Software |
| 36 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
| 37 |
** packaging of this file. Please review the following information to |
| 38 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
| 39 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 40 |
** |
| 41 |
** In addition, as a special exception, Nokia gives you certain |
| 42 |
** additional rights. These rights are described in the Nokia Qt LGPL |
| 43 |
** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this |
| 44 |
** package. |
| 45 |
** |
| 46 |
** GNU General Public License Usage |
| 47 |
** Alternatively, this file may be used under the terms of the GNU |
| 48 |
** General Public License version 3.0 as published by the Free Software |
| 49 |
** Foundation and appearing in the file LICENSE.GPL included in the |
| 50 |
** packaging of this file. Please review the following information to |
| 51 |
** ensure the GNU General Public License version 3.0 requirements will be |
| 52 |
** met: http://www.gnu.org/copyleft/gpl.html. |
| 53 |
** |
| 54 |
** Please note Third Party Software included with Qt Solutions may impose |
| 55 |
** additional restrictions and it is the user's responsibility to ensure |
| 56 |
** that they have met the licensing requirements of the GPL, LGPL, or Qt |
| 57 |
** Solutions Commercial license and the relevant license of the Third |
| 58 |
** Party Software they are using. |
| 59 |
** |
| 60 |
** If you are unsure which license is appropriate for your use, please |
| 61 |
** contact Nokia at [email protected]. |
| 62 |
** |
| 63 |
****************************************************************************/</span> |
| 64 |
|
| 65 |
#include <qtsingleapplication.h> |
| 66 |
#include <QtGui/QTextEdit> |
| 67 |
|
| 68 |
class TextEdit : public QTextEdit |
| 69 |
{ |
| 70 |
Q_OBJECT |
| 71 |
public: |
| 72 |
TextEdit(QWidget *parent = 0) |
| 73 |
: QTextEdit(parent) |
| 74 |
{} |
| 75 |
public slots: |
| 76 |
void append(const QString &str) |
| 77 |
{ |
| 78 |
QTextEdit::append(str); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
#include "main.moc" |
| 83 |
|
| 84 |
int main(int argc, char **argv) |
| 85 |
{ |
| 86 |
QtSingleApplication instance(argc, argv);</pre> |
| 87 |
<p>The example has only the <tt>main</tt> entry point function. A <a href="qtsingleapplication.html">QtSingleApplication</a> object is created immediately.</p> |
| 88 |
<pre> if (instance.sendMessage("Wake up!")) |
| 89 |
return 0;</pre> |
| 90 |
<p>If another instance of this application is already running, sendMessage() will succeed, and this instance just exits immediately.</p> |
| 91 |
<pre> TextEdit logview; |
| 92 |
logview.setReadOnly(true); |
| 93 |
logview.show();</pre> |
| 94 |
<p>Otherwise the instance continues as normal and creates the user interface.</p> |
| 95 |
<pre> instance.setActivationWindow(&logview); |
| 96 |
|
| 97 |
QObject::connect(&instance, SIGNAL(messageReceived(const QString&)), |
| 98 |
&logview, SLOT(append(const QString&))); |
| 99 |
|
| 100 |
return instance.exec();</pre> |
| 101 |
<p>The <tt>logview</tt> object is also set as the application's activation window. Every time a message is received, the window will be raised and activated automatically.</p> |
| 102 |
<p>The messageReceived() signal is also connected to the <a href="http://qt.nokia.com/doc/4.5/qtextedit.html">QTextEdit</a>'s append() slot. Every message received from further instances of this application will be displayed in the log.</p> |
| 103 |
<p>Finally the event loop is entered.</p> |
| 104 |
<p /><address><hr /><div align="center"> |
| 105 |
<table width="100%" cellspacing="0" border="0"><tr class="address"> |
| 106 |
<td width="30%" align="left">Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies)</td> |
| 107 |
<td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td> |
| 108 |
<td width="30%" align="right"><div align="right">Qt Solutions</div></td> |
| 109 |
</tr></table></div></address></body> |
| 110 |
</html> |