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

Flushing events on mainthread from worker thread while main thread is blocked



  • I have a scenario where a third-party library creates worker threads (not QThreads). The library calls a method on one of my objects to report progress. During this entire operation my main thread is blocked, but I need to update the progress bar on the main window.

    My progress function is therefore called from and executing in a worker thread. In the main thread, I have connected a signal in my class (containing the progress method) to a slot in the main window using a queued connection. The slots are not called until the control returns to the main GUI, which is when all of the work is done. What I need to do is after the worker thread emits the progress signal I need the worker thread to flushed the queued events on the main window (main thread). Calling sendPostedEvents() from the worker thread seems to have no effect. Any ideas anyone? Things go something like this:

    1. Main thread creates class A.
    2. Main thread has main window B.
    3. Main thread connects A::signal1 to B::slot1 using a queued connection.
    4. Third-party library function is called, main thread blocks.
    5. Worker thread emits A::signal1.
      ***It is at this point I need the main GUI to flush the queued signals (to get the GUI updated immediately), but neither sendPostedEvents() nor processEvents() work from the worker thread.

    Currently, all of the queued signals get processed once the main thread unblocks.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why is the main thread blocked ? Is it because of that 3rdparty library ? If so, why not handle it in a secondary thread ?


  • Qt Champions 2017

    @jhoove64 said in Flushing events on mainthread from worker thread while main thread is blocked:

    Any ideas anyone?

    You have implemented your threading wrong? Show some code and we may discuss it.

    Currently, all of the queued signals get processed once the main thread unblocks.

    Any blocking operations should be offloaded from the main thread. Blocking the main thread, means no events are processed, means no updates to the UI are done and no UI input is accepted.


Log in to reply