Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
When to use EventLoop exec and QApp exe
Pradeep P N last edited by
QApplication has exec function which starts the event loop. Also I can start the even loop using QEventLoop exec function. when should I used the QApp exec ? and when should i used the QEventLoop exec ?. Assume I have started the eventloop through QApp in main.cpp and inside the constructor I call exec through QEventLoop. What is the use starting another even loop constructor or somewhere else ?
There's rarely need for an explicit creation of your own QEventLoop instance.
Most of the places you would need it are covered by a convenience exec() method on a relevant class that will create and run one for you eg.
QApplication::exec - (almost) every Qt app needs QApplication instance and an event loop so this is just to type less.
QThread::exec - usually you want a thread to run its own event loop so the run method of QThread will call this exec and run one for you.
QDialog::exec - its often convenient to run a dialog in a separate loop that returns a value (accepted/rejected) so this will create one and run for you.
Usually the only place to use QEventLoop directly is when you are not covered by these scenarios and need a fine grained control in your local loop eg. use QEventLoop::exec parameters to discard specific events in that loop or create your own class that has this loop-return code pattern, create a local loop in a dynamically loaded library etc. but these are very rare occasions.