| 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"> </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) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 22 | ** All rights reserved. |
| 23 | ** |
| 24 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
| 25 | ** |
| 26 | ** This file is part of a Qt Solutions component. |
| 27 | ** |
| 28 | ** You may use this file under the terms of the BSD license as follows: |
| 29 | ** |
| 30 | ** "Redistribution and use in source and binary forms, with or without |
| 31 | ** modification, are permitted provided that the following conditions are |
| 32 | ** met: |
| 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 |
| 38 | ** distribution. |
| 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 |
| 42 | ** permission. |
| 43 | ** |
| 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." |
| 55 | ** |
| 56 | ****************************************************************************/</span> |
| 57 | |
| 58 | #include <qtsingleapplication.h> |
| 59 | #include <QtGui/QTextEdit> |
| 60 | |
| 61 | class TextEdit : public QTextEdit |
| 62 | { |
| 63 | Q_OBJECT |
| 64 | public: |
| 65 | TextEdit(QWidget *parent = 0) |
| 66 | : QTextEdit(parent) |
| 67 | {} |
| 68 | public slots: |
| 69 | void append(const QString &str) |
| 70 | { |
| 71 | QTextEdit::append(str); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | #include "main.moc" |
| 76 | |
| 77 | int main(int argc, char **argv) |
| 78 | { |
| 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!")) |
| 82 | return 0;</pre> |
| 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); |
| 86 | logview.show();</pre> |
| 87 | <p>Otherwise the instance continues as normal and creates the user interface.</p> |
| 88 | <pre> instance.setActivationWindow(&logview); |
| 89 | |
| 90 | QObject::connect(&instance, SIGNAL(messageReceived(const QString&)), |
| 91 | &logview, SLOT(append(const QString&))); |
| 92 | |
| 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> |
| 103 | </html> |