我发现我的应用程序的QThread.start()
不会像started()
那样发出documentation claims信号。我已将started()
信号连接到一个插槽,但它永远不会触发,除非我明确地调用started.emit()
。
我已经减少了我的代码,以便对问题进行可运行的演示。正如您所看到的,信号实际上是连接到插槽,而线程实际上是由start()
启动的,所以这些都不是问题所在。
有什么问题,started()
永远不会被释放出来?
#!/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.')
你的while
循环阻止了事件循环。 started
信号从另一个线程发出。在这种情况下,将使用排队连接,这意味着主线程需要检查事件队列以处理槽调用,但是你的while循环阻止了它。