[Solved] Singleton Qt app instance on host OS: How to get QGuiApplication executable invoked multiple times with parameters?

  • Hi all,

    I need to implement my QGuiApplication (i.e. using both QML and Qt C++ code) in the following way:

    1. Ensure only one instance of the app is running on the OS (Windows, Mac, Linux, etc). That is, if app is already running and the user clicks on the executable, the previous instance comes to focus

    2. Be able to invoke the same running process of the Qt app with parameters, from the command line. I am assuming that if #1 is met, this shouldn't be an issue.

    I would imagine that Windows and Mac API has some different means of doing this. But really hoping that Qt can make it easier.


  • Lifetime Qt Champion


    Have a look at QtSingleApplication, it should provide what you need to get started

  • Hi again,

    I'm trying to compile with QtSingleApplication now but having several issues:

    1. #include <QtSingleApplication>
      doesn't work even after I add in the .dll files to the Qt runtime folders:

    The QtSingleApplication library/source doesn't come with Qt 5 and I had to get it separately from: https://qt.gitorious.org/qt-solutions/qt-solutions/source/fd22bee22274975c56f1c10d87ee9fd2c0818f83:qtsingleapplication

    I managed to build the required .dll files with the "buildlib" Qt project file that comes with the repository. That is, I open the project file through Qt creator and "Build" it. It creates the .dll files in the "lib" folder and also Automatically, it copies them to my Qt runtime folders:

    Creating library ..\lib\QtSolutions_SingleApplication-headd.lib and object ..\lib\QtSolutions_SingleApplication-headd.exp
    if exist ..\lib\QtSolutions_SingleApplication-headd.dll_manifest.bak del ..\lib\QtSolutions_SingleApplication-headd.dll_manifest.bak
    copy /y "..\lib\QtSolutions_SingleApplication-headd.dll" ......\Qt\Qt5.2.1\5.2.1\msvc2012_64_opengl\bin
    1 file(s) copied.

    This was the only way I could build the .dll files. Running qmake, make or nmake doesn't work from my Windows command prompt.

    Am I supposed add paths to these dynamic libraries still, in my .pro file?
    (even though its already in my runtime environment)

    _Or am I supposed to copy and package the "qtsingleapplication.pri" file into my Qt app's project and include it in my .pro file? _

    One thing to note is that because of licensing issues, I probably can't just package the source files of QtSingleApplication.

    1. The documentation on QtSingleApplication repo says that it has only been tested up to Qt 4.5. So will it even work with the latest Qt 5, in place of QGuiApplication? (in an app using QML and C++)

    Thanks again.

  • I don't think you can directly apply it to QtGuiApplication. AFAIK, there were two flavours already, a QtSingleApplication and later also a QtSingleCoreApplication. As we now have yet another application type in between those two, I don't think you can use QtSingleApplication directly. It is a nice example of how subclassing is not always the best way to add functionality :)

    What you could do, is look into the sources of Qt(Core)Application to see what it does. The basic idea is not that difficult to re-create. You need to use some form or IPC: a local socket, a network port you listen on, whatever. You then check on startup if that channel is already there, and if so, send your message (with the startup parameters of the newly started application) to the already existing application, and then exit. The listening application should then simply handle the mesage.

  • Ah. That is a good idea. Yah, in that case, I should just implement a localhost TCP socket communication (on a fixed port number) and then check for that connection right when the app starts. Thanks :) I will change the subject of the post to be solved for now.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.