定义两个类:Receiver接收信号,等待2秒后执行。Sender发送信号触发Receiver槽函数的执行
class Receiver : public QObject {
Q_OBJECT
public slots:
void exec() {
QThread::sleep(1);
qDebug() << QThread::currentThreadId() << "receiver exec";
}
};
class Sender : public QObject {
Q_OBJECT
public:
void exec() {
emit sig();
qDebug() << QThread::currentThreadId() << "sender exec";
}
signals:
void sig();
};
将sender对象放入线程,但是显示调用sender对象的exec()方法,此时moveToThread不起作用,receiver和sender都在主线程中执行,sender发送信号时,会等待receiver的槽函数执行完毕才执行后续的输出
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Receiver* r = new Receiver();
Sender* s = new Sender();
QObject::connect(s, SIGNAL(sig()), r, SLOT(exec()));
QThread* thread = new QThread();
s->moveToThread(thread);
thread->start();
s->exec();
QThread::sleep(1000);
return a.exec();
}
将receiver放到单独的线程中,sender在主线程中发信号触发receiver的槽函数。此时默认槽函数连接方式Qt::AutoConnection等同于Qt::QueuedConnection,槽函数在子线程中执行,sender发送信号后立刻执行输出语句
额外的:
int main() {
Receiver* r = new Receiver();
Sender* s = new Sender();
QObject::connect(s, SIGNAL(sig()), r, SLOT(exec()));
QThread* thread = new QThread();
r->moveToThread(thread);
thread->start();
s->exec();
QThread::sleep(1000);
}
将Sender类的exec()函数改为循环执行5次,发送5次信号会直接执行完成。Receiver中的槽函数正常执行5次,执行过程不会被新来的信号打断
class Sender : public QObject {
Q_OBJECT
public:
void exec() {
for (int i = 0; i < 5; i++) {
emit sig();
qDebug() << QThread::currentThreadId() << "sender exec";
}
}
signals:
void sig();
};
Sender中一次发送两个信号,分别触发Receiver中的两个槽函数,一个延迟1s执行一个立即执行。槽函数按照发送信号的顺序执行。
class Sender : public QObject {
Q_OBJECT
public:
void exec() {
for (int i = 0; i < 5; i++) {
emit sig();
emit sig2();
qDebug() << QThread::currentThreadId() << "sender exec";
}
}
signals:
void sig();
void sig2();
};
class Receiver : public QObject {
Q_OBJECT
public slots:
void exec() {
QThread::sleep(1);
qDebug() << QDateTime::currentDateTime() << QThread::currentThreadId() << "receiver exec";
}
void exec2() {
qDebug() << QDateTime::currentDateTime() << QThread::currentThreadId() << "receiver exec2";
}
};
总结: 在Qt::QueuedConnection连接模式下,跨线程的槽函数调用会按照信号发送顺序执行。槽函数执行过程中不会被中断,槽函数执行完毕后再次开始事件循环,继续执行下一次的槽函数。来不及执行的信号和参数会被框架“保存”起来