Qt : Only One Program is Running

In many cases you may want to ensure that only one program or instance is running. In another word, if an instance of program is running and the user tries to run a new instance, you want to found that and exit second program and maybe show an appropriate message. You can implementated Qt  only one program or instance is running problem in Qt with libraray qtsingleapplication.

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.

This is an example how to create program with Qt  only one program or instance is running with library qtsingleapplication (you can get other example in source code qtsingleapplication).

#include <QtGui/QApplication>
#include <QMessageBox>
#include "runonlyone.h"  //change with your include window application
#include "qtsingleapplication.h"

int main(int argc, char *argv[])
{
    QtSingleApplication instance("qtsingleapplication sample", argc, argv);
    if ( instance.sendMessage("is running application!") )
    {
        QMessageBox msgBox;
        msgBox.setText("Your program is running.");
        msgBox.exec();
        return 0;
    }
    runOnlyOne w;  //change with your window application
    w.show();

    instance.setActivationWindow(&w);
    return instance.exec();
}

This is simple example hot to implementated Qt  only one program or instance is running. With this library, you can create your program that can running only one program or instance in Qt.

qtsingleapplication is part of Qt Solution Archive. You can Download the complete of Qt Solution Archive source code at http://qt.gitorious.org/qt-solutions.

Source :
http://qt.nokia.com/products/qt-addons/solutions-archive/
2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *