How should I quit a Qt Program, e.g when loading a data file, and discovered file corruption, and user need to quit this app or re-initiate data file?

Should I:

  1. call exit(EXIT_FAILURE)
  2. call QApplication::quit()
  3. call QCoreApplication::quit()

And difference between (2) and (3)?


QApplication is derived from QCoreApplication and thereby inherits quit() which is a public slot of QCoreApplication, so there is no difference between QApplication::quit() and QCoreApplication::quit().

As we can read in the documentation of QCoreApplication::quit() it "tells the application to exit with return code 0 (success).". If you want to exit because you discovered file corruption then you may not want to exit with return code zero which means success, so you should call QCoreApplication::exit() because you can provide a non-zero returnCode which, by convention, indicates an error.

It is important to note that "if the event loop is not running, this function (QCoreApplication::exit()) does nothing", so in that case you should call exit(EXIT_FAILURE).


You can call qApp.exit();. I always use that and never had a problem with it.

If you application is a command line application, you might indeed want to return an exit code. It's completely up to you what the code is.

  • Does not work in Qt5 – YumYumYum Feb 6 '17 at 1:55
  • 1
    Qt5 this worked QApplication::quit() – YumYumYum Feb 6 '17 at 1:56
  • 1
    I needed to do the following: qApp->exit(); – Frederick Ollinger Nov 8 '17 at 19:16

If you're using Qt Jambi, this should work:

//How to Run App

bool ok = QProcess::startDetached("C:\\TTEC\\CozxyLogger\\CozxyLogger.exe");
qDebug() <<  "Run = " << ok;

//How to Kill App

system("taskkill /im CozxyLogger.exe /f");
qDebug() << "Close";


  • Will be easier to read if you fix the code formatting. – Shashanth Jan 6 '18 at 8:15

if you need to close your application from main() you can use this code

QApplication app(argc, argv);
if(!QSslSocket::supportsSsl()) return app.exit(0);
return app.exec();

The program will terminated if OpenSSL is not installed

  • 1
    I think this does not compile. app.exit(0) does not return an int. Use if(!QSslSocket::supportsSsl()) {return 0;};. Because app.exec() has not been called at this line, app.exit(0) does nothing, see documentation of QCoreApplication::exit(): If the event loop is not running, this function does nothing. – Sacha Guyer May 5 '17 at 7:07

Your Answer

