Qt:向死线程/已停止线程发送信号

时间:2018-07-11 09:11:32

标签: c++ multithreading qt signals-slots qt-signals

我遇到了问题,我可能正在将信号从一个QThread发送到另一个slot,但是我无法检查实现Qt::QueuedConnection的线程是否正在运行。框架在使用... WorkerImp* pWorker = new WorkerImp(); QThread worker; pWorker->moveToThread(&worker); QObject::connect(&worker, QThread::finished, pWorker, &QObject::deleteLater, Qt::QueuedConnection); bool connected = QObject::connect(this, &SlaveMaster::requireWork, pWorker, &WorkerImp::doWork, Qt::QueuedConnection); assert(connected); // at this point we have connected the signal, thread is not starded. // however the object that we use to connect still exists and will after we exit the thread. worker.start(); worker.exit(); worker.wait(); // note that when we exit the QThread we do not destroy the object - it can be start over emit requireWork(); ... 时将如何处理这种情况?

        <textField>             
            <box>
                <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            </box>
            <textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
        </textField>

1 个答案:

答案 0 :(得分:2)

从不处理信号。线程之间的排队连接作为事件发布到线程,由事件循环处理。如果线程已停止(因此停止了其事件循环),则没有人可以拾取事件并将其传递:

来自Qt文档Signals and Slots Across Threads

  

排队连接:当控制权返回到接收者线程的事件循环时,将调用该插槽。该插槽在接收者的线程中执行。

Per-Thread Event Loop

  

[...]如果没有运行任何事件循环,则事件不会传递到该对象。

请注意,阻塞的排队连接将死锁