QThread 的 started() 信号没有发出
QThread's started() signal is not emitted
我发现我的应用 QThread.start()
不像 documentation claims 那样发出 started()
信号。我已将 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 循环正在阻止它。
我发现我的应用 QThread.start()
不像 documentation claims 那样发出 started()
信号。我已将 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 循环正在阻止它。