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?

-- 
Sybren A. Stüvel

syb...@stuvel.eu
http://stuvel.eu/

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to