[Interest] Relationship between a QEventLoop and QCoreApplication::exec().
Thiago Macieira
thiago.macieira at intel.com
Wed Aug 3 19:58:06 CEST 2016
On quarta-feira, 3 de agosto de 2016 11:22:36 PDT Jorge Fierro wrote:
> What exactly is the relationship between an event loop entered by
> calling QEventLoop::exec() and *the* main event loop (the one running
> when you can QCoreApplication::exec())?
Both functions as well as QThread::exec() are windows into the actual event
dispatcher that runs behind the scenes. All three control the
QAbstractEventDispatcher that either QCoreApplication or QThread creates.
I think these two links answer your question best:
https://code.woboq.org/qt5/qtbase/src/corelib/kernel/
qcoreapplication.cpp.html#_ZN16QCoreApplication4execEv (line 1258)
https://code.woboq.org/qt5/qtbase/src/corelib/thread/
qthread.cpp.html#_ZN7QThread4execEv (line 506)
As you can see, both QCoreApplication::exec or QThread::exec create a
QEventLoop and call exec on it. The same is also true, for that matter, for
QDialog::exec.
> There are a number of related questions:
>
> - Does QEventLoop::exec() enter *the* main event loop? Or,
> - Does QEventLoop::exec() enter a different event loop independent
> from QCoreApplication::exec()?
There's only one event dispatcher per thread.
> - Can QEventLoop::exec() be called outside QCoreApplication::exec()
> (i.e., in a call stack that didn't originate from QCoreApplication::exec())?
Sure. You could even use QEventLoop::exec() in place of
QCoreApplication::exec() in your main function or QThread::exec() in your run
function. The two are very similar, but not identical. QCoreApplication's loop
emits aboutToQuit and its descendant classes react to the last window closing,
while QEventLoop does not.
> - If QEventLoop::exec() spins the main loop then any event at all can
> be generated. Is this why one must use it very carefully to avoid
> reentrancy issues?
Yes. Avoid nesting event loops like the plague.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
More information about the Interest
mailing list