Search code examples
pythonpython-2.7pyqtpyqt4

How to pyqt4 gui used asynchronously?


This code stops GUI when the loop starts. And, text append operate end loop.

I want not stop GUI, and I want the text to come out in real time.

from PyQt4 import QtCore, QtGui
class MyFrom(QtGui.QMainWindow) :
    def __init__(self, parent=None) :
        QtGui.QWidget.__init__(self, parent)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.btn_start.clicked.connect(self.setOptionState)

    def setOptionState(self) :
        for i in xrange(5) :
            self.ui.textBrowser.append("[+] Case : %d\n" % i)
            self.testFunc()

    def testFunc(self) :
        for i in xrange(100000000) :
            pass

Solution

  • The GUI has a default loop and this is blocked by loops that consume too much time, and generate the application freeze or inappropriate behaviors, a simple way to solve is to use processEvents()

    def testFunc(self) :
        for i in xrange(1000000) :
            QtGui.qApp.processEvents()
            pass