Skip to content Skip to sidebar Skip to footer

How Do I Shut Down PyQt's QtApplication Correctly?

I don't know the first thing about Qt, but I'm trying to be cheeky and borrow code from elsewhere (http://lateral.netmanagers.com.ar/weblog/posts/BB901.html#disqus_thread). ;) I h

Solution 1:

You need to handle the QApplication outside of the test functions, sort of like a singleton (it's actually appropriate here).

What you can do is to check if QtCore.qApp is something (or if QApplication.instance() returns None or something else) and only then create your qApp, otherwise, use the global one.

It will not be destroyed after your test() function since PyQt stores the app somewhere.

If you want to be sure it's handled correctly, just setup a lazily initialized singleton for it.


Solution 2:

A QApplication should only be initialized once! It can be used by as many Capture instances as you like, but you should start them in the mainloop. See: https://doc.qt.io/qt-4.8/qapplication.html

You could also try "del app" after "app.exec_", but I am unsure about the results. (Your original code runs fine on my system)

I would use urllib instead of webkit:

import urllib

class Capturer:
    def capture(self, s_url, s_filename):
        s_file_out, httpmessage = urllib.urlretrieve(s_url, s_filename, self.report)

    def report(self, i_count, i_chunk, i_size):
        print('retrived %5d of %5d bytes' % (i_count * i_chunk, i_size))

def test():
    c = Capturer()
    c.capture("http://www.google.com/google.png", "google1.png")
    c.capture("http://www.google.com/google.png", "google2.png")

if __name__ == '__main__':
    test()

Post a Comment for "How Do I Shut Down PyQt's QtApplication Correctly?"