Qt跨线程的槽函数执行

2023-12-21 22:09:48

定义两个类: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();
};

1. moveToThread(),但是直接调用

将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();
}

2. 槽函数在子线程中执行

将receiver放到单独的线程中,sender在主线程中发信号触发receiver的槽函数。此时默认槽函数连接方式Qt::AutoConnection等同于Qt::QueuedConnection,槽函数在子线程中执行,sender发送信号后立刻执行输出语句

额外的:

  1. 子线程必须通过start()开启,否则receiver
  2. 如果指定槽函数的连接方式为Qt::DirectConnection,槽函数也会在主线程中执行
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);
}

3. 信号循环发送,但槽函数执行速度慢

将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();
};

在这里插入图片描述

4. 一次触发两个槽函数

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连接模式下,跨线程的槽函数调用会按照信号发送顺序执行。槽函数执行过程中不会被中断,槽函数执行完毕后再次开始事件循环,继续执行下一次的槽函数。来不及执行的信号和参数会被框架“保存”起来

文章来源:https://blog.csdn.net/darkLight2017/article/details/135118833
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。