Miam-Player  0.8.0
A nice music player
QtSingleApplication Class Reference

The QtSingleApplication class provides an API to detect and communicate with running instances of an application. More...

#include <qtsingleapplication.h>

Inheritance diagram for QtSingleApplication:

Public Slots

void aboutToSendArgs (const QString &message)
 
void activateWindow ()
 De-minimizes, raises, and activates this application's activation window. More...
 
bool forwardArgsToServer (int timeout=5000)
 

Signals

void messageReceived (const QString &message)
 This signal is emitted when the current instance receives a message from another instance of this application. More...
 
void sendArgs (const QStringList &)
 

Public Member Functions

 QtSingleApplication (int &argc, char **argv, bool GUIenabled=true)
 Creates a QtSingleApplication object. More...
 
 QtSingleApplication (const QString &id, int &argc, char **argv)
 Creates a QtSingleApplication object with the application identifier appId. More...
 
bool isRunning ()
 Returns true if another instance of this application is running; otherwise false. More...
 
QString id () const
 Returns the application identifier. More...
 
void setActivationWindow (QMainWindow *aw, bool activateOnMessage=true)
 Sets the activation window of this application to aw. More...
 
QWidget * activationWindow () const
 Returns the applications activation window if one has been set by calling setActivationWindow(), otherwise returns 0. More...
 

Detailed Description

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

Constructor & Destructor Documentation

QtSingleApplication::QtSingleApplication ( int &  argc,
char **  argv,
bool  GUIenabled = true 
)

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.

QtSingleApplication::QtSingleApplication ( const QString &  appId,
int &  argc,
char **  argv 
)

Creates a QtSingleApplication object with the application identifier appId.

argc and argv are passed on to the QAppliation constructor.

Member Function Documentation

void QtSingleApplication::aboutToSendArgs ( const QString &  message)
slot
void QtSingleApplication::activateWindow ( )
slot

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(), initialize()
QWidget * QtSingleApplication::activationWindow ( ) const

Returns the applications activation window if one has been set by calling setActivationWindow(), otherwise returns 0.

See also
setActivationWindow()
bool QtSingleApplication::forwardArgsToServer ( int  timeout = 5000)
slot
QString QtSingleApplication::id ( ) const

Returns the application identifier.

Two processes with the same identifier will be regarded as instances of the same application.

bool QtSingleApplication::isRunning ( )

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()
void QtSingleApplication::messageReceived ( const QString &  message)
signal

This signal is emitted when the current instance receives a message from another instance of this application.

See also
sendMessage(), setActivationWindow(), activateWindow()
void QtSingleApplication::sendArgs ( const QStringList &  )
signal
void QtSingleApplication::setActivationWindow ( QMainWindow *  aw,
bool  activateOnMessage = true 
)

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
activateWindow(), messageReceived()

The documentation for this class was generated from the following files: