[PyQt] Disposing of a QApplication

Mads Ipsen mpi at comxnet.dk
Fri Mar 12 13:30:09 GMT 2010


Sybren A. Stüvel wrote:
> Hi all,
>
> I'm working on a PyQt GUI application using PyQt 4.6 and Python 2.6. According 
> to the documentation:
>
> 	For any GUI application using Qt, there is precisely one QApplication object
>
> This is true for our application, but not for our unit tests. They start and 
> stop a QApplication in quick succession, so when the test stops it has to shut 
> down and dispose of the application properly. The application instance is 
> stored in 'self.app' of the unittest class, but just doing 'del self.app' 
> doesn't destroy the application (probably because there are other references 
> to it in the Qt modules).
>
> Right now I start a separate thread that waits for a bit and then sends the 
> application the quit() signal. Then I start the application's main loop, so 
> that it is running when it receives the quit() signal from the other thread. 
> Of course, this is not an ideal way.
>
> I tried a single-threaded approach by first sending it the quit() signal and 
> then run app.exec_() but that doesn't stop the application. Another approach I 
> used was to try and replace the event loop with the following code, but that 
> also doesn't stop the application.
>
>         def aboutToQuit():
>             self.fail("Quitting!")
>         
>         self.app.aboutToQuit.connect(aboutToQuit)
>
>         # Send the quit event and let the application process it.
>         start_time = time.time()
>         while not self.app.closingDown():
>             self.app.quit()
>             self.app.processEvents()
>             self.app.sendPostedEvents(self.app, 0)
>             self.app.flush()
>             
>             time.sleep(0.01)
>             
>             # Wait three seconds for the app to stop.
>             if time.time() - start_time > 3.0:
>                 self.fail("Failed to quit the application")
>         
>         del self.app
>         self.fail("Done")
>
> It's done in a unittest.TestCase class, and the self.fail() calls are there so 
> that I can quickly see what's going on.
>
> What would you recommend?
>
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
We have a module that contains one instance of the QApplication object

QT_APP = QtGui.QApplication(sys.argv)

All unit tests include this module - I believe this makes QT_APP a 
single object.

Best regards,

Mads


-- 
+-------------------------------------------------------------+
| Mads Ipsen, Scientific developer                            |
+-------------------------------+-----------------------------+
| QuantumWise A/S               | phone:         +45-29716388 |
| Nørre Søgade 27A              | www:    www.quantumwise.com |
| DK-1370 Copenhagen K, Denmark | email:  mpi at quantumwise.com |
+-------------------------------+-----------------------------+




More information about the PyQt mailing list