⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

PySide: QtSingleApplication?

  • Hi there,

    I would like to have only one instance of my application, so I found the wonderful QtSingleApplication.
    However, it seems not to be included in PySide so how can we do that with PySide?

    I would like not just to prevent the application to be opened twice or more, but to send the application parameters to the currently running application instead of starting a new one.

    Is it possible?

  • QtSingleAppliction is not part of official Qt, then PySide does not create binding for that. I have implemented something like that using a QLocalSocket and QLocalServer.

    The basic idea is:

    // try to connect to your server
    m_socket = QLocalSocket()
    m_socket.connectToServer(qApp.applicationName(), QIODevice.ReadOnly)

    //If this fail is because the app is not running then create the server, then in the next time the connect will work
    m_server = QLocalServer()
    m_server = QLocalServer.listen(qApp.applicationName())

  • OK, thanks for your help.

    Then I guess I have to use the buffer between the new application (client) and the first launched application (server) to send the new application arguments (using write() and read()), is it?

    I'll try it, thanks again!

  • It works very well, thank you!

    To go further, it is detected that the application is already currently running, is to possible to put that window on top of the other windows?

    I tried with QMainWindow.activateWindow() but it does not put the window on top of the other windows.
    Is there any way to do it?

  • I shared this recipe on my blog, with a QSingleApplication class for PySide that allows you to be sure your program will be started only once.
    In addition to start the application only once, we can send the arguments of the later calls of your program to the first (and only remaining) instance.

    Link: http://www.dallagnese.fr/en/computers-it/recette-python-qt4-qsingleapplication-pyside/

Log in to reply