gpt4 book ai didi

c++ - 带线程的 Qt 信号槽

转载 作者:搜寻专家 更新时间:2023-10-31 02:20:00 26 4
gpt4 key购买 nike

我在 QThread 类中遇到信号/槽问题。我的设计看起来像这样:

class Manager : public QObject {
Q_OBJECT
public:
Manager(QObject* parent) : QObject(parent) {
Thread thread* = new Thread(this);
connect(this, SIGNAL(testsignal()), thread, SLOT(test()));
thread->start();

...

emit testsignal();
}
signals:
void testsignal();
};

class Thread : public QThread {
Q_OBJECT
public slots:
void test() {
qDebug() << "TEST";
}
private:
void run() {}
};

信号永远不会到达我的 test() 方法。有人可以帮忙吗?谢谢。

最佳答案

问题是 sending signals across threads导致将信号排队到目标线程的事件队列(排队的连接)中。如果该线程从不处理事件,它就永远不会收到信号。

此外,according to the QThread::run documentation :

Returning from this method will end the execution of the thread.

换句话说,空的 run 方法会导致线程立即终止,因此您正在向死线程发送信号。

关于c++ - 带线程的 Qt 信号槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33215596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com