【发布时间】:2019-04-09 07:12:34
【问题描述】:
我发现我的应用程序的QThread.start() 不会像documentation claims 那样发出started() 信号。我已将started() 信号连接到一个插槽,但除非我明确调用started.emit(),否则它永远不会触发。
我已将代码缩减为问题的可运行演示。可以看到,signal其实是连接到slot的,线程其实是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.')
【问题讨论】:
-
@thuga 哦,天哪,这太明显了。我想我今天没有想。如果您将其作为答案,我会将其标记为已接受。