Debian Codegraph @

thirdparty/singleapp/doc/html/qtsingleapplication-example-trivial.html

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