关于QEventLoop的理解

测试代码
cpp 复制代码
	QTimer* timer = new QTimer;
	timer->setInterval(1000);

	QThread* thread = new QThread();

	timer->moveToThread(thread);
	thread->moveToThread(thread);
	connect(thread, &QThread::started, timer, QOverload<>::of(&QTimer::start));
	thread->start();

	
	QObject::connect(timer, &QTimer::timeout, this, [=]() {
		int t = 0;
		qDebug() << "chufa";
		count += 1;
		QEventLoop loop;
		QtConcurrent::run([&]() {
			for (int i = 0; i < 100; i++) {
				t += 1;
				qDebug() << t << QThread::currentThreadId();
				QThread::msleep(1);
			}

			loop.quit();
			});
		if(count > 3)
			QMetaObject::invokeMethod(timer, "stop");
		loop.exec();
		qDebug() << u8"执行完成";
		});
测试结果
  1. 如果启用事件循坏,Ui事件不会阻塞,但是QTimer的信号会阻塞,为了测试我才把QTimer移动到子线程。但是为什么QTimer信号会阻塞这个不清楚,可能是loop没有包含QTimer事件处理
  2. 虽然loop.exec()会阻塞后面的代码延迟执行,但是并不会阻止timeout信号多次触发槽函数并执行槽函数。所以这里并没有把线程给阻塞。
  3. loop.exec();后面的代码想执行必须是所有的loop都quit后才会一起执行,并不是某一个loop退出后就会执行某一次后面的代码
相关推荐
我是菜鸟0713号3 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_3 天前
QT(4)
开发语言·汇编·c++·qt·算法
lqjun08273 天前
Qt程序单独运行报错问题
开发语言·qt
酷飞飞3 天前
Qt Designer与事件处理
开发语言·qt·命令模式
mkhase3 天前
9.12-QT-基本登陆界面实现
java·jvm·qt
咕噜咕噜啦啦3 天前
Qt之快捷键、事件处理、自定义按键——完成记事本项目
开发语言·qt
Quz3 天前
QML Charts组件之折线图的鼠标交互
qt
眠りたいです3 天前
基于脚手架微服务的视频点播系统-数据管理与网络通信部分的预备工作
c++·qt·ui·微服务·云原生·架构·媒体
bikong73 天前
Qt/C++,windows多进程demo
c++·windows·qt
油炸自行车4 天前
【Qt】Window环境下搭建Qt6、MSVC2022开发环境(无需提前安装Visual Studio)
qt·visual studio·qt6·msvc2022·qt creator 17.0