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 Trivial 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 Trivial Example
<br /><span class=
"subtitle"></span>
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.6/qtextedit.html">QTextEdit
</a> log.
</p>
19 <pre><span class=
"comment"> /****************************************************************************
21 ** Copyright (C)
2010 Nokia Corporation and/or its subsidiary(-ies).
22 ** All rights reserved.
24 ** Contact: Nokia Corporation (qt-info@nokia.com)
26 ** This file is part of a Qt Solutions component.
28 ** You may use this file under the terms of the BSD license as follows:
30 **
"Redistribution and use in source and binary forms, with or without
31 ** modification, are permitted provided that the following conditions are
33 ** * Redistributions of source code must retain the above copyright
34 ** notice, this list of conditions and the following disclaimer.
35 ** * Redistributions in binary form must reproduce the above copyright
36 ** notice, this list of conditions and the following disclaimer in
37 ** the documentation and/or other materials provided with the
39 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
40 ** the names of its contributors may be used to endorse or promote
41 ** products derived from this software without specific prior written
44 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45 **
"AS IS
" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
47 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
48 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"
56 ****************************************************************************/
</span>
58 #include
<qtsingleapplication.h
>
59 #include
<QtGui/QTextEdit
>
61 class TextEdit : public QTextEdit
65 TextEdit(QWidget *parent =
0)
69 void append(const QString
&str)
71 QTextEdit::append(str);
75 #include
"main.moc
"
77 int main(int argc, char **argv)
79 QtSingleApplication instance(argc, argv);
</pre>
80 <p>The example has only the
<tt>main
</tt> entry point function. A
<a href=
"qtsingleapplication.html">QtSingleApplication
</a> object is created immediately.
</p>
81 <pre> if (instance.sendMessage(
"Wake up!
"))
83 <p>If another instance of this application is already running, sendMessage() will succeed, and this instance just exits immediately.
</p>
84 <pre> TextEdit logview;
85 logview.setReadOnly(true);
87 <p>Otherwise the instance continues as normal and creates the user interface.
</p>
88 <pre> instance.setActivationWindow(
&logview);
90 QObject::connect(
&instance, SIGNAL(messageReceived(const QString
&)),
91 &logview, SLOT(append(const QString
&)));
93 return instance.exec();
</pre>
94 <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>
95 <p>The messageReceived() signal is also connected to the
<a href=
"http://qt.nokia.com/doc/4.6/qtextedit.html">QTextEdit
</a>'s append() slot. Every message received from further instances of this application will be displayed in the log.
</p>
96 <p>Finally the event loop is entered.
</p>
97 <p /><address><hr /><div align=
"center">
98 <table width=
"100%" cellspacing=
"0" border=
"0"><tr class=
"address">
99 <td width=
"30%" align=
"left">Copyright
© 2010 Nokia Corporation and/or its subsidiary(-ies)
</td>
100 <td width=
"40%" align=
"center"><a href=
"http://qt.nokia.com/doc/trademarks.html">Trademarks
</a></td>
101 <td width=
"30%" align=
"right"><div align=
"right">Qt Solutions
</div></td>
102 </tr></table></div></address></body>