[PySide] Fwd: Creating and deleting instances of QApplication within unit tests

Matthew Woehlke matthew.woehlke at kitware.com
Fri Oct 25 22:09:46 CEST 2013

On 2013-10-25 15:13, Dan McCombs wrote:
> I've been struggling with unit testing my PySide application. My tests run
> fine, but if I have more than one test, Python segfaults on quit. It seems
> the solution would be to destroy/create the QApplication instance on each
> test run, as I've seen people mentioning in the case of PyQT such as:
> http://stuvel.eu/blog/127/multiple-instances-of-qapplication-in-one-process
> I've tried doing something similar in PySide with the following lines in my
> setUp for each test:
> QtGui.qApp = QtGui.QApplication([])

Um... this wouldn't work in C++, and I rather suspect it's not the right 
way to go about it in Python either.

In C++, qApp is an alias for QApplication::instance(). I believe the 
case for Python is similar. Probably when you assign to QtGui.qApp in 
Python you are replacing the convenience accessor with a reference to 
your own instance, which does nothing to actually get rid of the 
QApplication instance.

Normally you should just construct a QApplication instance and assign it 
to some variable.

Did you try this?

   app = QtGui.QApplication([])
   delete app


More information about the PySide mailing list