May 8, 2011

ThomasDalla ThomasDalla
Lab Rat
21 posts

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?

4 replies

May 8, 2011

renato.filho renato.filho
Hobby Entomologist
52 posts

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())

May 9, 2011

ThomasDalla ThomasDalla
Lab Rat
21 posts

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!

May 9, 2011

ThomasDalla ThomasDalla
Lab Rat
21 posts

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?

May 10, 2011

ThomasDalla ThomasDalla
Lab Rat
21 posts

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/

 
  ‹‹ qt and c#      [SOLVED] Pyside, Pil image to QPixmap ››

You must log in to post a reply. Not a member yet? Register here!