Ungraceful / kill Qt application when event loop is not started

4.6k views Asked by At

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?

1

There are 1 answers

4
Jarra McIntyre On BEST ANSWER

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;
}