关于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退出后就会执行某一次后面的代码
相关推荐
richard_yuu26 分钟前
图像融合与多帧平均提SNR
qt·opencv·计算机视觉
ShineWinsu2 小时前
对于Qt:QSS样式属性的解析
开发语言·c++·qt·面试·盒子模型·客户端·qss
小灰灰搞电子5 小时前
Qt 输入验证入门:QValidator 类详解
开发语言·qt
凤年徐1 天前
Qt 第三章 常用控件总结
开发语言·qt
草莓熊Lotso1 天前
Qt 进阶控件实战:下拉框、列表、表格 + 旋钮 / 滑动条全攻略
大数据·c语言·开发语言·c++·人工智能·qt
迎風吹頭髮1 天前
QT中使用QSqlTableModel快速展示SQLite3数据表(TableView实现)
开发语言·qt·sqlite
ShineWinsu1 天前
对于Qt:QMediaPlayer和QVideoWidget的解析
开发语言·c++·qt·视频·客户端·qmediaplayer·媒体播放器
li星野1 天前
Qt 单例配置管理器学习记录:JsonConfigManager 的设计原理与使用示例
开发语言·qt·学习
蓝蓝的代码人2 天前
用 Python、Qt 和 Vue 做一个 Windows 股票看盘工具,观潮的行情小窗与桌面提醒
vue.js·python·qt·开源·桌面应用
ShineWinsu2 天前
对于Qt:QSound的解析
c++·qt·面试·音频·笔试·客户端·qsound