Overriding QThread.run and calling the default implementation

Maurizio Berti maurizio.berti at gmail.com
Fri Jul 2 18:55:53 BST 2021


After some testing to dig deeper in the QThread subject, pushed by the
previous related thread, I found something I don't really understand (just
to add something more to the "you never stop learning").

Take the following simple example:

from PyQt5 import QtCore, QtWidgets
class Worker(QtCore.QThread):
    def run(self):
        super().run()

class Test(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        layout = QtWidgets.QVBoxLayout(self)
        self.button = QtWidgets.QPushButton('start')
        layout.addWidget(self.button)
        self.label = QtWidgets.QLabel('waiting')
        layout.addWidget(self.label)

        self.worker = Worker()

        self.button.clicked.connect(self.start)

    def start(self):
        self.label.setText('started')
        self.button.setEnabled(False)
        self.worker.start()
import sys
app = QtWidgets.QApplication(sys.argv)
w = Test()
w.show()
sys.exit(app.exec_())


This results in freezing the whole application as soon as the thread is
started.
Why does this happen? I believe it could be related to how threading works
and the whole wrapping, but I'm not sure and I'd like to understand the
reason for this anyway.

If instead of calling run() I call exec() (which is what run() actually
does), it works fine.

-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210702/f5937935/attachment-0001.htm>


More information about the PyQt mailing list