Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Request processor time for the execution of a slot



  • I have an application that has two threads running - the main (GUI) one and a helper/worker one. Some of the work is offloaded to the second thread, so that the GUI is more responsive but there is some code which belongs to objects residing in the main thread, that I want to be executed with greater priority. The GUI construction during application startup takes some seconds (4-5) and during that time the worker thread emits signals caught by the main thread's objects that I want to be executed with higher priority than GUI's construction and thus pause it for a while. I tried putting:
    @
    // ...
    QCoreApplication::processEvents();
    // ...
    @

    in the slot that belongs to the object residing in the main thread or to put:
    @
    // ...
    qApp->thread()->eventDispatcher()->processEvents( QEventLoop::AllEvents );
    // ...
    @

    in the function of the object that belongs to the worker thread, immediately after the signal mentioned above is emitted. By the latter call I want the main thread to turn its attention to the signal that was just emitted and process its event loop's events. I want to postpone the execution of GUI-related events but didn't found the right
    @
    QEventLoop::ProcessEventsFlag
    @

    One of the flags makes so that user input events are skipped:
    @
    QEventLoop::ExcludeUserInputEvents
    @

    but haven't found one to skip UI construction, for instance.

    Any suggestions?



  • If signal and slot are in different threads, they are executed asynchronously (via event queue). Therefore, if your initialization code runs for 4-5 seconds before the event loop kicks in, no code you can put into the slot will help you.

    Trying to kick-start the main event loop from a different thread is also doomed to fail. You just can't interrupt the main thread in mid-execution and dispatch events - at worst, total chaos might ensue.

    My suggestion is to find out what actually takes the 4-5 seconds. Optimize it, and split it into multiple smaller initialization parts. You can call processEvents between those initialization parts. That way, you clearly define when you can interrupt the initialization process without causing harm, and still get your tasks done long before the initialization is finished.


Log in to reply