关于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退出后就会执行某一次后面的代码
相关推荐
blasit8 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
范特西.i5 天前
QT聊天项目(8)
开发语言·qt
枫叶丹45 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
十五年专注C++开发6 天前
Qt deleteLater作用及源码分析
开发语言·c++·qt·qobject
kangzerun6 天前
SQLiteManager:一个优雅的Qt SQLite数据库操作类
数据库·qt·sqlite
金刚狼886 天前
qt和qt creator的下载安装
开发语言·qt
追烽少年x6 天前
Qt中使用Zint库显示二维码
qt
谁刺我心6 天前
qt源码、qt在线安装器镜像下载
开发语言·qt
金刚狼886 天前
在qt creator中创建helloworld程序并构建
开发语言·qt
扶尔魔ocy6 天前
【转载】QT使用linuxdeployqt打包
开发语言·qt