I have an application with a thread performing a lengthy task. I trigger the lengthy task using a QPushButton which emits a signal. However, clicking the button makes the GUI unresponsive for the length of the process even though that process is in another thread. Here is a small program replicating this behavior:
import sys
import time
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import pyqtSignal, QThread, Qt
class WriteThread(QThread):
write_signal = pyqtSignal()
def __init__(self):
super().__init__()
self.write_signal.connect(self.worker, Qt.QueuedConnection)
def worker(self):
print("Before sleep")
time.sleep(2)
print("After sleep")
return True
class Example(QWidget):
def __init__(self):
super().__init__()
self.CustomEvent = None
self.write_thread = None
self.init_ui()
def init_ui(self):
self.write_thread = WriteThread()
redb = QPushButton('Red', self)
redb.move(10, 10)
blueb = QPushButton('Blue', self)
blueb.move(10, 50)
blueb.clicked.connect(self.print_method)
redb.clicked.connect(self.send_event)
self.write_thread.start()
self.setGeometry(300, 300, 300, 250)
self.setWindowTitle('Toggle button')
self.show()
def send_event(self):
print("\tSending signal")
self.write_thread.write_signal.emit()
print("\tFinished sending signal")
def print_method(self):
print("Not frozen")
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
The time.sleep(2) simulates the lengthy process.
My question really is: why does the process which is in a different thread freeze the GUI?