关于QEventLoop的理解

Source
测试代码
	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退出后就会执行某一次后面的代码