1

How can I terminate a running Qt application (QCoreApplication) when exit does not work because the event loop is not yet started.

http://doc.qt.io/qt-5/qcoreapplication.html#exit

After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode. If the event loop is not running, this function does nothing

One (stupid?) approach I have found is to start the event loop and call QCoreApplication::exit again , but is this my best option?

Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • do you really have to kill it this early? can it wait? – user3528438 Feb 22 '16 at 00:50
  • It is a really rare, hopefully never existing condition. I replies to a severe error situation during application startup and will most likely be handled by `exit()`But what do I place in the line behind exit if it will ever be passed? – Horst Walter Feb 22 '16 at 00:55

1 Answers1

1

If I am understanding your question correctly, you have not yet called QApplication::exec(), therefore your event loop has not started.

If you have not yet called exec() to start the event loop why not just call the stdlib exit() function or check the error condition before calling exec()

E.g. in main.cpp

if(!somethingWentReallyWrong) {
    a.exec(); // Where a is your QApplication or QCoreApplication instance
} else {
    return myErrorCode;
}
Jarra McIntyre
  • 1,265
  • 8
  • 13
  • Will try the stdlib exec(). Possible approach. I cannot terminate in main, if have to terminate in some function (for some special reasons). – Horst Walter Feb 22 '16 at 01:11
  • @HorstWalter "Will try the stdlib exec()" => you mean exit() ? BTW what are you special reasons ? – Ilya Feb 22 '16 at 09:16
  • `sts::exit`is meant. The use case is a own application class (specialized `QApplication`) where in rare cases I need to exit before the event loop is started. – Horst Walter Feb 22 '16 at 16:58
  • @HorstWalter if you're happy with the answer please accept :) – Jarra McIntyre Feb 22 '16 at 22:15