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

Why this works: QCoreApplication::processEvents()



  • Hey guys, so I am trying to figure out why it is that when I use QCoreApplication::processEvents() my program works but if I don't it doesn't work correctly.

    Basically, I have no idea what the problem is. In my c++ code I am sending a signal, then I am processing an event, then I am calling a function, then I am emitting another signal:

          emit reconfiguring(true);
          //QCoreApplication::processEvents();
          changeMountLocation(location);
          emit reconfiguring(false);
    

    based on that signal I am creating a popup in my QML. However, if I don't un-comment the line QCoreApplication::processEvents() then the function is run prior to the popup showing up, and if I do un-comment that line then the popup shows up when it should.

    This means that, for some reason, the UI is waiting on the back end for something.... but what? I am not asking for anything, and I can't think of an easy way to figure out why its hanging...

    any ideas?


  • Lifetime Qt Champion

    @RobM said in Why this works: QCoreApplication::processEvents():

    This means that, for some reason, the UI is waiting on the back end for something

    Qt is an event driven framework. That means that there is a event loop which takes next event from the queue each time it is active. The UI will only be updated if the event loop has a chance to handle the events. In you case you emit a signal and call changeMountLocation, so event loop has no chance to run. QCoreApplication::processEvents() lets event loop do its work before you call changeMountLocation.



  • @jsulm So it is possible for an event on the event queue to be popped after the next line of code is being ran? I guess I just assumed it would work sequentially. Is there anyway for me to watch the event queue in real time?


  • Lifetime Qt Champion

    @RobM said in Why this works: QCoreApplication::processEvents():

    So it is possible for an event on the event queue to be popped after the next line of code is being ran?

    There is a workaround if you really want to do it sequentially: call https://doc.qt.io/qt-5/qcoreapplication.html#processEvents after emitting the signal.


Log in to reply