A question about QEventLoop.
Unsolved
General and Desktop
-
Hello everyone,
#include <QApplication> #include <QEventLoop> #include <QTimer> #include <QMetaObject> #include <QThread> int main(int argc, char* argv[]) { QApplication app(argc, argv); auto obj = new QObject(); auto th = new QThread(); th->start(); obj->moveToThread(th); for (size_t i = 0; i < 5; i++) { QThread::msleep(500); QMetaObject::invokeMethod(obj, [=]() { printf("%d start ", i); QEventLoop loop; QTimer::singleShot(5000, &loop, &QEventLoop::quit); loop.exec(); //QThread::sleep(10); printf("%d end ",i ); }); } return app.exec(); }
The printed result of this code is "0 start 1 start 2 start 3 start 4 start 4 end 3 end 2 end 1 end 0 end",
But as expected, the eventloop of 0 should be the first to end, so the 0 end should be printed first.