Correct way to quit a Qt program?

167,653

Solution 1

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).

Solution 2

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.

Solution 3

While searching this very question I discovered this example in the documentation.

QPushButton *quitButton = new QPushButton("Quit");
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection);

Mutatis mutandis for your particular action of course.

Along with this note.

It's good practice to always connect signals to this slot using a QueuedConnection. If a signal connected (non-queued) to this slot is emitted before control enters the main event loop (such as before "int main" calls exec()), the slot has no effect and the application never exits. Using a queued connection ensures that the slot will not be invoked until after control enters the main event loop.

It's common to connect the QGuiApplication::lastWindowClosed() signal to quit()

Solution 4

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

QApplication.closeAllWindows();
Share:
167,653
daisy
Author by

daisy

Updated on December 17, 2021

Comments

  • daisy
    daisy over 2 years

    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)?

  • Sacha Guyer
    Sacha Guyer about 7 years
    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.
  • Frederick Ollinger
    Frederick Ollinger over 6 years
    I needed to do the following: qApp->exit();
  • rookie coder
    rookie coder over 4 years
    the question is not about detaching new processes
  • Nico Haase
    Nico Haase over 3 years
    Please add some explanation to your answer such that others can learn from it