1

I have created a pyqt4 app and I want to make it so only one instance (of QApplication) is allowed to run.

The program reads and writes audio files, and if more than 1 instance is running, Windows (linux is fine) throws errors that 2 programs are trying to access the same files. I see a lot of java and C apps that will display a simple dialog if the program is already running, I just want to know how to do this in pyqt4.

A little help?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
randomlinuxmod
  • 327
  • 4
  • 12

2 Answers2

2

This kind of programming pattern is called a "singleton" instance or a "singleton application".

Usually it is done with a global mutex or by locking a file early in the life of the program. And when you program launches, if the file handle is already locked, then you exit.

Qt Solutions has it here: http://doc.qt.digia.com/solutions/4/qtsingleapplication/qtsingleapplication.html

https://qt.gitorious.org/qt-solutions/qt-solutions/source/841982ceec9d30a7ab7324979a0fd5c9c36fd121:qtsingleapplication

It would probably take a bit of work to get those global mutexes/locks to work in pyqt, since pyqt doesn't have the qt-solutions part in it yet as far as I could tell.

Here is an alternative that uses a cross platform python script:

Python: single instance of program

Hope that helps.

Community
  • 1
  • 1
phyatt
  • 18,472
  • 5
  • 61
  • 80
1

Thanks. I Used https://gitorious.org/qsingleapplication/qsingleapplication/source/ca13324b0f5bdfcaf4e379a78108f0bd85fed98a:qSingleApplication.py#L66 And Called QSingleApplication On My MainWindow And Works Fine

randomlinuxmod
  • 327
  • 4
  • 12