In Qt, is a QThread event loop loop executed when a slot is executed in a QObject belonging to the event loop thread?

I would like to confirm that, in my opinion, is a direct aspect of workflows in Qt.

Suppose I create a QThread whose purpose is to manage the time-consuming work in the corresponding thread. Also, suppose I allow this thread to start the corresponding event loop by calling start() in QThread. The work itself is performed by a member function (slot), which is signaled by the QThread started() signal.

That is (copying from https://stackoverflow.com/a/330965/ ... ):

 class Task : public QObject { Q_OBJECT public: Task(); ~Task(); public slots: void doWork() { //very time-consuming code is executed here before the next line is reached... emit workFinished(); // Calls workFinished() signal (after a long time) } signals: void workFinished(); }; // ... in the main thread: QThread *thread = new QThread( ); Task *task = new Task(); task->moveToThread(thread); connect( thread, SIGNAL(started()), task, SLOT(doWork()) ); connect( task, SIGNAL(workFinished()), thread, SLOT(quit()) ); //automatically delete thread and task object when work is done: connect( thread, SIGNAL(finished()), task, SLOT(deleteLater()) ); connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) ); thread->start(); 

My question is the following. I understand that a workflow event loop will receive a trigger from the finished() signal to call the deleteLater() slot on the task instance. In addition, this event loop will be launched shortly after the doWork() function doWork() , and therefore it will be ready and available for processing a trigger from the finished() signal, which was simply added to the workflow event queue by calling finished() at the end of the doWork()

I would like to confirm that during the entire duration of the temporary operation inside doWork() (before finished() is released and before the doWork() function doWork() ), the event loop inside the workflow is locked in the doWork() slot function, and therefore, the workflow will NOT respond to any slots that run on any objects that belong to the event loop thread; during the course, the doWork() time-consuming function is doWork() . (And therefore, any such slots will only be executed after doWork() exits after the doWork() event loop is active again and before the trigger from the finished() signal is processed.)

I suspect this is so, but I want to confirm.

Thanks!

+6
source share
1 answer

The workflow event loop will be blocked, i.e. you will not be able to handle any events (including those used to "queue" the connections between signals and slots, what you get, slots through streams), unless you explicitly call the event loop object yourself.

However, you can still have slots running in the workflow caused by signals that are emitted from the workflow, because these are simple function calls and don't need a triggered event loop.

+7
source

Source: https://habr.com/ru/post/946799/


All Articles