【问题标题】:QThread's started() signal is not emittedQThread 的started() 信号没有发出
【发布时间】: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 哦,天哪,这太明显了。我想我今天没有想。如果您将其作为答案,我会将其标记为已接受。

标签: qt qthread pyside2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多