I have an odd problem with QThreads in (pyqt). I tested QThread with implementing run method and running an infinite loop inside it. it does not affect main thread (gui). but when I emit a signal like below pseudo code any long running loop or i/o affect on main thread and makes gui freeze.
class MyThread(QThread):
def __init__(self , *args):
QThread.__init__(self , *args)
self.connect(self , SIGNAL("do_some_io(QString)") , self.doSomething)
def doSomething(self , params):
#do some large i/o and loops
parent.emit( SIGNAL("process_done()") )
class MyDialog(QDialog):
def __init__(self , *args):
QThread.__init__(self , *args)
self.Thread = MyThread(self)
self.Thread.start()
self.connect(self.btn , SIGNAL("clicked()") , self.buttonClicked)
self.connect(self , SIGNAL("process_done()") , self.showMsgBox)
def buttonClicked(self):
self.Thread.emit( SIGNAL("do_some_io(QString)") , "param" )
def showMsgBox(self):
#show messagebox
This is because when you emit do_some_io
, doSomething
is executed in the GUI thread. In your connect calls, you need to set the connection type to QueuedConnection. That will cause the message to be added as an event to the receiving thread event queue and it will then run the signal in the receiving thread instead of the sending thread.