From 5300c9dd1261a7f4ff8bf5a83608470e4953dd0f Mon Sep 17 00:00:00 2001
From: Gluzskiy Alexandr The QtSingleApplication component provides support for applications that can be only started once per user. For some applications it is useful or even critical that they are started only once by any user. Future attempts to start the application should activate any already running instance, and possibly perform requested actions, e.g. loading a file, in that instance. The QtSingleApplication class provides an interface to detect a running instance, and to send command strings to that instance. For console (non-GUI) applications, the QtSingleCoreApplication variant is provided, which avoids dependency on QtGui. The application in this example loads or prints the documents passed as commandline parameters to further instances of this application. The user interface in this application is a QMainWindow subclass with a QMdiArea as the central widget. It implements a slot handleMessage() that will be connected to the messageReceived() signal of the QtSingleApplication class. The MainWindow constructor creates a minimal user interface. The handleMessage() slot interprets the message passed in as a filename that can be prepended with /print to indicate that the file should just be printed rather than loaded. Loading the file will also activate the window. The main entry point function creates a QtSingleApplication object, and creates a message to send to a running instance of the application. If the message was sent successfully the process exits immediately. If the message could not be sent the application starts up. Note that false is passed to the call to setActivationWindow() to prevent automatic activation for every message received, e.g. when the application should just print a file. Instead, the message handling function determines whether activation is requested, and signals that by emitting the needToShow() signal. This is then simply connected directly to QtSingleApplication's activateWindow() slot. The application in this example has a log-view that displays messages sent by further instances of the same application. The example demonstrates the use of the QtSingleApplication 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 QTextEdit log. The example has only the main entry point function. A QtSingleApplication object is created immediately. If another instance of this application is already running, sendMessage() will succeed, and this instance just exits immediately. Otherwise the instance continues as normal and creates the user interface. The logview object is also set as the application's activation window. Every time a message is received, the window will be raised and activated automatically. The messageReceived() signal is also connected to the QTextEdit's append() slot. Every message received from further instances of this application will be displayed in the log. Finally the event loop is entered. This is the complete list of members for QtSingleApplication, including inherited members.
+
+
+ Home
+Single Application
+
+
+Description
+Classes
+
+
+Examples
+
+
+Tested platforms
+
+
+
+ Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)
+Trademarks
+
+
+
+
+ Home
+Loading Documents
+
+ /****************************************************************************
+ **
+ ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+ ** All rights reserved.
+ **
+ ** Contact: Nokia Corporation (qt-info@nokia.com)
+ **
+ ** This file is part of a Qt Solutions component.
+ **
+ ** You may use this file under the terms of the BSD license as follows:
+ **
+ ** "Redistribution and use in source and binary forms, with or without
+ ** modification, are permitted provided that the following conditions are
+ ** met:
+ ** * Redistributions of source code must retain the above copyright
+ ** notice, this list of conditions and the following disclaimer.
+ ** * Redistributions in binary form must reproduce the above copyright
+ ** notice, this list of conditions and the following disclaimer in
+ ** the documentation and/or other materials provided with the
+ ** distribution.
+ ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+ ** the names of its contributors may be used to endorse or promote
+ ** products derived from this software without specific prior written
+ ** permission.
+ **
+ ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+ **
+ ****************************************************************************/
+
+ #include <qtsingleapplication.h>
+ #include <QtCore/QFile>
+ #include <QtGui/QMainWindow>
+ #include <QtGui/QPrinter>
+ #include <QtGui/QPainter>
+ #include <QtGui/QTextEdit>
+ #include <QtGui/QMdiArea>
+ #include <QtCore/QTextStream>
+
+ class MainWindow : public QMainWindow
+ {
+ Q_OBJECT
+ public:
+ MainWindow();
+
+ public slots:
+ void handleMessage(const QString& message);
+
+ signals:
+ void needToShow();
+
+ private:
+ QMdiArea *workspace;
+ };
+ MainWindow::MainWindow()
+ {
+ workspace = new QMdiArea(this);
+
+ setCentralWidget(workspace);
+ }
+ void MainWindow::handleMessage(const QString& message)
+ {
+ enum Action {
+ Nothing,
+ Open,
+ Print
+ } action;
+
+ action = Nothing;
+ QString filename = message;
+ if (message.toLower().startsWith("/print ")) {
+ filename = filename.mid(7);
+ action = Print;
+ } else if (!message.isEmpty()) {
+ action = Open;
+ }
+ if (action == Nothing) {
+ emit needToShow();
+ return;
+ }
+
+ QFile file(filename);
+ QString contents;
+ if (file.open(QIODevice::ReadOnly))
+ contents = file.readAll();
+ else
+ contents = "[[Error: Could not load file " + filename + "]]";
+
+ QTextEdit *view = new QTextEdit;
+ view->setPlainText(contents);
+
+ switch(action) {
+ case Print:
+ {
+ QPrinter printer;
+ view->print(&printer);
+ delete view;
+ }
+ break;
+
+ case Open:
+ {
+ workspace->addSubWindow(view);
+ view->setWindowTitle(message);
+ view->show();
+ emit needToShow();
+ }
+ break;
+ default:
+ break;
+ };
+ }
+ #include "main.moc"
+
+ int main(int argc, char **argv)
+ {
+ QtSingleApplication instance("File loader QtSingleApplication example", argc, argv);
+ QString message;
+ for (int a = 1; a < argc; ++a) {
+ message += argv[a];
+ if (a < argc-1)
+ message += " ";
+ }
+
+ if (instance.sendMessage(message))
+ return 0;
+ MainWindow mw;
+ mw.handleMessage(message);
+ mw.show();
+
+ QObject::connect(&instance, SIGNAL(messageReceived(const QString&)),
+ &mw, SLOT(handleMessage(const QString&)));
+
+ instance.setActivationWindow(&mw, false);
+ QObject::connect(&mw, SIGNAL(needToShow()), &instance, SLOT(activateWindow()));
+
+ return instance.exec();
+ }
+
+ Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)
+Trademarks
+
+
+
+
+ Home
+A Trivial Example
+
+ /****************************************************************************
+ **
+ ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+ ** All rights reserved.
+ **
+ ** Contact: Nokia Corporation (qt-info@nokia.com)
+ **
+ ** This file is part of a Qt Solutions component.
+ **
+ ** You may use this file under the terms of the BSD license as follows:
+ **
+ ** "Redistribution and use in source and binary forms, with or without
+ ** modification, are permitted provided that the following conditions are
+ ** met:
+ ** * Redistributions of source code must retain the above copyright
+ ** notice, this list of conditions and the following disclaimer.
+ ** * Redistributions in binary form must reproduce the above copyright
+ ** notice, this list of conditions and the following disclaimer in
+ ** the documentation and/or other materials provided with the
+ ** distribution.
+ ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+ ** the names of its contributors may be used to endorse or promote
+ ** products derived from this software without specific prior written
+ ** permission.
+ **
+ ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+ **
+ ****************************************************************************/
+
+ #include <qtsingleapplication.h>
+ #include <QtGui/QTextEdit>
+
+ class TextEdit : public QTextEdit
+ {
+ Q_OBJECT
+ public:
+ TextEdit(QWidget *parent = 0)
+ : QTextEdit(parent)
+ {}
+ public slots:
+ void append(const QString &str)
+ {
+ QTextEdit::append(str);
+ }
+ };
+
+ #include "main.moc"
+
+ int main(int argc, char **argv)
+ {
+ QtSingleApplication instance(argc, argv);
+ if (instance.sendMessage("Wake up!"))
+ return 0;
+ TextEdit logview;
+ logview.setReadOnly(true);
+ logview.show();
+ instance.setActivationWindow(&logview);
+
+ QObject::connect(&instance, SIGNAL(messageReceived(const QString&)),
+ &logview, SLOT(append(const QString&)));
+
+ return instance.exec();
+
+ Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)
+Trademarks
+
+
+
+
+ Home
+List of All Members for QtSingleApplication
+
+
+
+
+
+
Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | +Trademarks | +Qt Solutions |
+
![]() |
+Home | +
The following class members are obsolete. They are provided to keep old source code working. We strongly advise against using them in new code.
+ +void | initialize ( bool dummy = true ) (obsolete) |
Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | +Trademarks | +Qt Solutions |
+
![]() |
+Home | +
The QtSingleApplication class provides an API to detect and communicate with running instances of an application. More...
+#include <QtSingleApplication>
Inherits QApplication.
+ +QtSingleApplication ( int & argc, char ** argv, bool GUIenabled = true ) | |
QtSingleApplication ( const QString & appId, int & argc, char ** argv ) | |
QtSingleApplication ( int & argc, char ** argv, Type type ) | |
QtSingleApplication ( Display * dpy, Qt::HANDLE visual = 0, Qt::HANDLE cmap = 0 ) | |
QtSingleApplication ( Display * dpy, int & argc, char ** argv, Qt::HANDLE visual = 0, Qt::HANDLE cmap = 0 ) | |
QtSingleApplication ( Display * dpy, const QString & appId, int argc, char ** argv, Qt::HANDLE visual = 0, Qt::HANDLE cmap = 0 ) | |
QWidget * | activationWindow () const |
QString | id () const |
bool | isRunning () |
void | setActivationWindow ( QWidget * aw, bool activateOnMessage = true ) |
void | activateWindow () |
bool | sendMessage ( const QString & message, int timeout = 5000 ) |
void | messageReceived ( const QString & message ) |
The QtSingleApplication class provides an API to detect and communicate with running instances of an application.
+This class allows you to create applications where only one instance should be running at a time. I.e., if the user tries to launch another instance, the already running instance will be activated instead. Another usecase is a client-server system, where the first started instance will assume the role of server, and the later instances will act as clients of that server.
+By default, the full path of the executable file is used to determine whether two processes are instances of the same application. You can also provide an explicit identifier string that will be compared instead.
+The application should create the QtSingleApplication object early in the startup phase, and call isRunning() to find out if another instance of this application is already running. If isRunning() returns false, it means that no other instance is running, and this instance has assumed the role as the running instance. In this case, the application should continue with the initialization of the application user interface before entering the event loop with exec(), as normal.
+The messageReceived() signal will be emitted when the running application receives messages from another instance of the same application. When a message is received it might be helpful to the user to raise the application so that it becomes visible. To facilitate this, QtSingleApplication provides the setActivationWindow() function and the activateWindow() slot.
+If isRunning() returns true, another instance is already running. It may be alerted to the fact that another instance has started by using the sendMessage() function. Also data such as startup parameters (e.g. the name of the file the user wanted this new instance to open) can be passed to the running instance with this function. Then, the application should terminate (or enter client mode).
+If isRunning() returns true, but sendMessage() fails, that is an indication that the running instance is frozen.
+Here's an example that shows how to convert an existing application to use QtSingleApplication. It is very simple and does not make use of all QtSingleApplication's functionality (see the examples for that).
+// Original + int main(int argc, char **argv) + { + QApplication app(argc, argv); + + MyMainWidget mmw; + mmw.show(); + return app.exec(); + } + + // Single instance + int main(int argc, char **argv) + { + QtSingleApplication app(argc, argv); + + if (app.isRunning()) + return !app.sendMessage(someDataString); + + MyMainWidget mmw; + app.setActivationWindow(&mmw); + mmw.show(); + return app.exec(); + }+
Once this QtSingleApplication instance is destroyed (normally when the process exits or crashes), when the user next attempts to run the application this instance will not, of course, be encountered. The next instance to call isRunning() or sendMessage() will assume the role as the new running instance.
+For console (non-GUI) applications, QtSingleCoreApplication may be used instead of this class, to avoid the dependency on the QtGui library.
+See also QtSingleCoreApplication.
+Creates a QtSingleApplication object. The application identifier will be QCoreApplication::applicationFilePath(). argc, argv, and GUIenabled are passed on to the QAppliation constructor.
+If you are creating a console application (i.e. setting GUIenabled to false), you may consider using QtSingleCoreApplication instead.
+Creates a QtSingleApplication object with the application identifier appId. argc and argv are passed on to the QAppliation constructor.
+Creates a QtSingleApplication object. The application identifier will be QCoreApplication::applicationFilePath(). argc, argv, and type are passed on to the QAppliation constructor.
+Special constructor for X11, ref. the documentation of QApplication's corresponding constructor. The application identifier will be QCoreApplication::applicationFilePath(). dpy, visual, and cmap are passed on to the QApplication constructor.
+Special constructor for X11, ref. the documentation of QApplication's corresponding constructor. The application identifier will be QCoreApplication::applicationFilePath(). dpy, argc, argv, visual, and cmap are passed on to the QApplication constructor.
+Special constructor for X11, ref. the documentation of QApplication's corresponding constructor. The application identifier will be appId. dpy, argc, argv, visual, and cmap are passed on to the QApplication constructor.
+De-minimizes, raises, and activates this application's activation window. This function does nothing if no activation window has been set.
+This is a convenience function to show the user that this application instance has been activated when he has tried to start another instance.
+This function should typically be called in response to the messageReceived() signal. By default, that will happen automatically, if an activation window has been set.
+See also setActivationWindow(), messageReceived(), and initialize().
+Returns the applications activation window if one has been set by calling setActivationWindow(), otherwise returns 0.
+See also setActivationWindow().
+Returns the application identifier. Two processes with the same identifier will be regarded as instances of the same application.
+Returns true if another instance of this application is running; otherwise false.
+This function does not find instances of this application that are being run by a different user (on Windows: that are running in another session).
+See also sendMessage().
+This signal is emitted when the current instance receives a message from another instance of this application.
+See also sendMessage(), setActivationWindow(), and activateWindow().
+Tries to send the text message to the currently running instance. The QtSingleApplication object in the running instance will emit the messageReceived() signal when it receives the message.
+This function returns true if the message has been sent to, and processed by, the current instance. If there is no instance currently running, or if the running instance fails to process the message within timeout milliseconds, this function return false.
+See also isRunning() and messageReceived().
+Sets the activation window of this application to aw. The activation window is the widget that will be activated by activateWindow(). This is typically the application's main window.
+If activateOnMessage is true (the default), the window will be activated automatically every time a message is received, just prior to the messageReceived() signal being emitted.
+See also activationWindow(), activateWindow(), and messageReceived().
+Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | +Trademarks | +Qt Solutions |
+
![]() |
+Home | +
This example shows how to use the single-application functionality in a console application. It does not require the QtGui library at all.
+The only differences from the GUI application usage demonstrated in the other examples are:
+1) The .pro file should include qtsinglecoreapplication.pri instead of qtsingleapplication.pri
+2) The class name is QtSingleCoreApplication instead of QtSingleApplication.
+3) No calls are made regarding window activation, for obvious reasons.
+console.pro:
+TEMPLATE = app + CONFIG += console + SOURCES += main.cpp + include(../../src/qtsinglecoreapplication.pri) + QT -= gui+
main.cpp:
+ /****************************************************************************
+ **
+ ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+ ** All rights reserved.
+ **
+ ** Contact: Nokia Corporation (qt-info@nokia.com)
+ **
+ ** This file is part of a Qt Solutions component.
+ **
+ ** You may use this file under the terms of the BSD license as follows:
+ **
+ ** "Redistribution and use in source and binary forms, with or without
+ ** modification, are permitted provided that the following conditions are
+ ** met:
+ ** * Redistributions of source code must retain the above copyright
+ ** notice, this list of conditions and the following disclaimer.
+ ** * Redistributions in binary form must reproduce the above copyright
+ ** notice, this list of conditions and the following disclaimer in
+ ** the documentation and/or other materials provided with the
+ ** distribution.
+ ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+ ** the names of its contributors may be used to endorse or promote
+ ** products derived from this software without specific prior written
+ ** permission.
+ **
+ ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+ **
+ ****************************************************************************/
+
+ #include "qtsinglecoreapplication.h"
+ #include <QtCore/QDebug>
+
+ void report(const QString& msg)
+ {
+ qDebug("[%i] %s", (int)QCoreApplication::applicationPid(), qPrintable(msg));
+ }
+
+ class MainClass : public QObject
+ {
+ Q_OBJECT
+ public:
+ MainClass()
+ : QObject()
+ {}
+
+ public slots:
+ void handleMessage(const QString& message)
+ {
+ report( "Message received: \"" + message + "\"");
+ }
+ };
+
+ int main(int argc, char **argv)
+ {
+ report("Starting up");
+
+ QtSingleCoreApplication app(argc, argv);
+
+ if (app.isRunning()) {
+ QString msg(QString("Hi master, I am %1.").arg(QCoreApplication::applicationPid()));
+ bool sentok = app.sendMessage(msg, 2000);
+ QString rep("Another instance is running, so I will exit.");
+ rep += sentok ? " Message sent ok." : " Message sending failed; the other instance may be frozen.";
+ report(rep);
+ return 0;
+ } else {
+ report("No other instance is running; so I will.");
+ MainClass mainObj;
+ QObject::connect(&app, SIGNAL(messageReceived(const QString&)),
+ &mainObj, SLOT(handleMessage(const QString&)));
+ return app.exec();
+ }
+ }
+
+ #include "main.moc"
+Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | +Trademarks | +Qt Solutions |
+
![]() |
+Home | +
This is the complete list of members for QtSingleCoreApplication, including inherited members.
+
|
|
Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | +Trademarks | +Qt Solutions |
+
![]() |
+Home | +
A variant of the QtSingleApplication class for non-GUI applications. More...
+#include <QtSingleCoreApplication>
Inherits QCoreApplication.
+ +QtSingleCoreApplication ( int & argc, char ** argv ) | |
QtSingleCoreApplication ( const QString & appId, int & argc, char ** argv ) | |
QString | id () const |
bool | isRunning () |
bool | sendMessage ( const QString & message, int timeout = 5000 ) |
void | messageReceived ( const QString & message ) |
A variant of the QtSingleApplication class for non-GUI applications.
+This class is a variant of QtSingleApplication suited for use in console (non-GUI) applications. It is an extension of QCoreApplication (instead of QApplication). It does not require the QtGui library.
+The API and usage is identical to QtSingleApplication, except that functions relating to the "activation window" are not present, for obvious reasons. Please refer to the QtSingleApplication documentation for explanation of the usage.
+A QtSingleCoreApplication instance can communicate to a QtSingleApplication instance if they share the same application id. Hence, this class can be used to create a light-weight command-line tool that sends commands to a GUI application.
+See also QtSingleApplication.
+Creates a QtSingleCoreApplication object. The application identifier will be QCoreApplication::applicationFilePath(). argc and argv are passed on to the QCoreAppliation constructor.
+Creates a QtSingleCoreApplication object with the application identifier appId. argc and argv are passed on to the QCoreAppliation constructor.
+Returns the application identifier. Two processes with the same identifier will be regarded as instances of the same application.
+Returns true if another instance of this application is running; otherwise false.
+This function does not find instances of this application that are being run by a different user (on Windows: that are running in another session).
+See also sendMessage().
+This signal is emitted when the current instance receives a message from another instance of this application.
+See also sendMessage().
+Tries to send the text message to the currently running instance. The QtSingleCoreApplication object in the running instance will emit the messageReceived() signal when it receives the message.
+This function returns true if the message has been sent to, and processed by, the current instance. If there is no instance currently running, or if the running instance fails to process the message within timeout milliseconds, this function return false.
+See also isRunning() and messageReceived().
+Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | +Trademarks | +Qt Solutions |
+