没有发出QThread的starts()信号

时间:2019-04-09 07:12:34

标签: qt qthread pyside2

我发现我的应用程序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.')

1 个答案:

答案 0 :(得分:2)

您的shrinkwrap循环阻止了事件循环。 while信号从另一个线程发出。在那种情况下,将使用排队连接,这意味着主线程需要去检查事件队列以处理插槽调用,但是您的while循环阻止了它。