关于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退出后就会执行某一次后面的代码
相关推荐
用户805533698035 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner5 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz10 天前
QML Hello World 入门示例
qt
xcyxiner13 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner14 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner14 天前
DicomViewer (添加模型类)3
qt
xcyxiner15 天前
DicomViewer (目录调整) 2
qt
xcyxiner15 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能17 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G17 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt