QEventLoop and QCoreApplication::processEvents



  • Hi,
    is there i difference between:

    QEventLoop loop;
    loop.exec();
    

    and

    while (true)  {
       QCoreApplication::processEvents();
    }
    

    Thanks!


  • Qt Champions 2016

    Well QEventLoop is a local event loop so
    there exists 2 at that moment in time while
    "processEvents" just drives the default one.

    Its best if you can design your application so no local waiting is needed. :)


  • Qt Champions 2016

    @beecksche said in QEventLoop and QCoreApplication::processEvents:

    is there i difference

    Yes, a huge difference. For one QEventLoop::exec is a "blocking" call, which means it will stop at that position and sleep until there are events to process. Finally if there's a call to QEventLoop::quit (through a signal-slot connection) it will return control to the user code.

    On the other hand QCoreApplication::processEvents() will process as many events there are in the event loop at the time of call and return control to the user code immediately (on first possible occasion).

    @mrjj said in QEventLoop and QCoreApplication::processEvents:

    Well QEventLoop is a local event loop

    Eh, mostly yes. The distinction is made by a single integer that counts the "nest level" of event loops. And as recently discussed in another thread the "local" event loop will spin the global one too. Actually, beside some tweaks like the QCoreApplication::aboutToQuit signal, QCoreApplication::exec just calls QEventLoop::exec.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.