I've discovered that my app's QThread.start()
doesn't emit the started()
signal as the documentation claims. I've connected the started()
signal to a slot, but it never fires unless I explicitly call started.emit()
.
I've pared down my code to a runnable demonstration of the problem. As you can see, the signal is actually connected to the slot, and the thread is actually started by start()
, so neither of those are the problem.
What's wrong with it that started()
is never emitted?
#!/usr/bin/env python3
import PySide2.QtCore
import PySide2.QtWidgets
@PySide2.QtCore.Slot()
def test_receiver():
print('thread.started() signal received.')
if __name__ == '__main__':
app = PySide2.QtWidgets.QApplication()
app.processEvents()
thread = PySide2.QtCore.QThread()
thread.started.connect(test_receiver)
thread.start()
# The connection between signal and slot isn't the problem because
# the signal has actually connected, as evidenced if you uncomment the following line:
#
# thread.started.emit()
#
# So why is thread.started() never emitted after thread.start()?
while thread.isRunning():
print('Thread is running...')
PySide2.QtCore.QThread.sleep(1)
print('Everything quit.')
Your while
loop is blocking the event loop. started
signal is emitted from the other thread. And in that case a queued connection will be used, which means that the main thread needs to go to check the event queue to handle the slot call, but your while loop is blocking it.