A simple single instance application for Qt
- Allows to run an application as "single instance"
- This means only one instance at a time of the application will be running
- Subsequent starts send their arguments to the running instance, instead of starting a new one
- Instances are generated per user
- It supports all kinds of applications, with or without gui.
- For gui/widgets applications, you can register a window to be activated
The package is providet as qpm package, de.skycoder42.qsingleinstance
. To install:
- Install qpm (See GitHub - Installing, for windows see below)
- In your projects root directory, run
qpm install de.skycoder42.qsingleinstance
- Include qpm to your project by adding
include(vendor/vendor.pri)
to your.pro
file
Check their GitHub - Usage for App Developers to learn more about qpm.
Important for Windows users: QPM Version 0.10.0 (the one you can download on the website) is currently broken on windows! It's already fixed in master, but not released yet. Until a newer versions gets released, you can download the latest dev build from here:
- https://storage.googleapis.com/www.qpm.io/download/latest/windows_amd64/qpm.exe
- https://storage.googleapis.com/www.qpm.io/download/latest/windows_386/qpm.exe
Check the three example projects to see what you can do for each application type. The following shows a simple console-based example:
#include <qsingleinstance.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSingleInstance instance;
//this lambda only gets executed on the first instance
instance.setStartupFunction([&]() -> int {
qDebug() << "Instance running! Start another with -quit as first argument to exit";
return 0;
});
//new instances send their arguments and this signal is received on the main instance
QObject::connect(&instance, &QSingleInstance::instanceMessage, [&](QStringList args){
qDebug() << "New Instance:" << args;
if(args.size() > 1 && args[1] == "-quit")
qApp->quit();
});
return instance.singleExec();
}