我有两个从QThread继承的线程。该场景是thread2发出信号的对象,而thread1的对象有一个要执行的槽。我期望插槽在thread2中执行,但它在主线程中执行!!!这是我的示例代码,用于显示:
Headrs.h
class Thread1 : public QThread{
Q_OBJECT
public:
Thread1(){}
protected:
void run(){
qDebug() << "run in thread " << this->currentThreadId();
exec();
}
private slots:
void slot1(){
qDebug() << "slot in " << this->currentThreadId();
}
signals:
void sig1();
};
class Thread2 : public QThread{
Q_OBJECT
protected:
void run(){
msleep(100);
qDebug() << "emit sig2 in: " << this->currentThreadId();
emit sig2();
}
signals:
void sig2();
};
class obj1 : public QObject {
Q_OBJECT
public:
obj1(){
connect(&t2, SIGNAL(sig2()), &t1, SLOT(slot1()));
connect(this, SIGNAL(sigObj()), &t1, SLOT(slot1()));
t1.start();
t2.start();
}
public:
void fcn(){
QThread::msleep(1000);
emit sigObj();
qDebug() << "emit sigObj in " << QThread::currentThreadId();
}
private:
Thread1 t1;
Thread2 t2;
signals:
void sigObj();
};
Main.cpp的
QCoreApplication a(argc, argv);
obj1 o1;
o1.fcn();
return a.exec();
我对此代码的期望是slot1()始终从发出的信号sig2()和sigObj()中执行thread1。但无论在哪个线程中我们发出信号,slot1都在主线程中执行。顺便说一下,这是我的输出:
run in thread 0x7ffff6169700 (thread1)
emit sig2 in: 0x7ffff5968700 (thread2)
slot in 0x7ffff7fce740 (main thread)
emit sigObj in 0x7ffff7fce740 (main thread)
slot in 0x7ffff7fce740 (main thread)
这是错误还是总是这样?如果我想在自己的线程中执行插槽,我该怎么办?
答案 0 :(得分:3)
QThread
没有什么特别之处。只有QObject
发生才能成为平台本地线程的句柄。但是QThread
派生对象的插槽就像你有一个普通的QObject
一样。它们将在对象的线程中执行 - 并且它将是执行对象构造函数的线程,或者在您的情况下,是主线程。这是令人困惑的地方:任何一个线程对象的thread()
仍然是主线程,以及插槽将运行的位置。仅仅因为QObject
被称为QThread
并不会使其变得与众不同。
修复很简单:不要覆盖QThread
&#34;&#34;运行&#34;,并且不要向QThread
添加功能。相反,明确地将对象移动到您希望它们完成工作的线程。它是一个反模式,可以在moveToThread
上明确调用QThread
,因此不要将其作为&#34;快速破解&#34;。
在这种情况下,从QThread
派生的唯一原因是通过向其析构函数添加quit(); wait();
将其转换为正确的RAII类。
以下是您的代码的外观,修复:
// https://github.com/KubaO/stackoverflown/tree/master/questions/thread-sigslot-37325348
#include <QtCore>
class Worker1 : public QObject {
Q_OBJECT
public:
Q_SIGNAL void sig1();
Q_SLOT void slot1() {
qDebug() << "slot in" << thread();
}
};
class Worker2 : public QObject {
Q_OBJECT
public:
Worker2() {
QTimer::singleShot(100, this, [this]{
qDebug() << "emit sig2 in" << thread();
emit sig2();
});
}
Q_SIGNAL void sig2();
};
class Object : public QObject {
Q_OBJECT
Worker1 w1;
Worker2 w2;
QThread t1, t2;
Q_SIGNAL void sig();
public:
Object() {
t1.setObjectName("t1");
t2.setObjectName("t2");
connect(&w2, &Worker2::sig2, &w1, &Worker1::slot1);
connect(this, &Object::sig, &w1, &Worker1::slot1);
w1.moveToThread(&t1);
w2.moveToThread(&t2);
t1.start();
t2.start();
QTimer::singleShot(1000, this, [this]{
qDebug() << "emit sig in" << thread();
emit sig();
});
}
~Object() { t1.quit(); t2.quit(); t1.wait(); t2.wait(); }
};
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
app.thread()->setObjectName("main_thread");
Object obj;
QTimer::singleShot(2000, &app, [&]{ app.quit(); });
return app.exec();
}
#include "main.moc"
输出:
emit sig2 in QThread(0x7fff4fd98bd0, name = "t2")
slot in QThread(0x7fff4fd98bc0, name = "t1")
emit sig in QThread(0x7fe3dac0aed0, name = "main_thread")
slot in QThread(0x7fff4fd98bc0, name = "t1")