1

I am working on a project which uses Qt for GUI development. Qt event loop is started in the main thread. But I have requirement to do some cleanup activities after the QApplication exits.

So I have used qApp->quit() for exiting the application and to confirm the successful exit of the QApplication, I am relying on the return value of the qApp->closingDown() as follows

if ( true == qApp->closingDown())
{
    //Successfull exit of the QApplication. Do post exit operations
}

Questions: a. Does qApp->quit() immediately makes qApp->closingDown() function to return the true value. b. Are there any other way to confirm the successful exit of the QApplication?

SRaju
  • 31
  • 3
  • 1
    You might want to take a look at `qAddPostRoutine`. See http://qt-project.org/doc/qt-5/qcoreapplication.html#qAddPostRoutine – RA. Aug 15 '14 at 23:00
  • Define "successful exit of the QApplication". – hyde Aug 16 '14 at 04:48

1 Answers1

0

Depending on just what you mean by "exit successfully", the most straightforward way is to examine the return value of the exec(). You can control it by using exit(int returnCode) instead of quit() to exit your application.

Since you want to wait for all QObjects to be destructed, one way to do that easily is to wrap QApplication into a scope, something like:

int main(int argc, char *argv[]) {
    int returnCode = 127; // choose whatever sentinel value
    {
        // put all Qt stuff in this scope
        QApplication app(argc, argv);
        //...
        returnCode = app.exec();
        // if there are any raw pointer heap QObjects without parent, delete them here
    }

    if (returnCode != 0) {
        std::cerr << "Error, exit code: " << returnCode << std::endl;
    }

    // do whatever cleanup you want to do after Qt stuff has been shut down
    // or signal the other thread, or whatever

    return returnCode;
}

But, as it is said in the docs of exec(), application might be killed before it has a chance to return from event loop, even in "normal" use when computer is shut down. But I'm not sure you can do much about that, other than connecting aboutToQuit() signal to catch exit earlier and perhaps do something like flush all open files etc, to avoid data corruption.

In case you're not happy with doing it like that, there's also the good old atexit().

hyde
  • 60,639
  • 21
  • 115
  • 176
  • The succesful exit in my context is, all QObjects must be destroyed and QApplication shouldn't process any events. – SRaju Aug 18 '14 at 16:41
  • The succesful exit in my context is, all QObjects must be destroyed and QApplication shouldn't process any events. In our application, We need to call the "exit(someExitCode)" from different thread which inturn runs the registered exit handlers for cleanup activities. To call exit() fromother thread, other thread must be sure that QApplication is exited ( event loop is exited) and no more events are processed in Qt event loop. So does qAppPostRoutine helps in this regard? Or are there any best way for my problem context? Thanking You Regards – SRaju Aug 18 '14 at 16:47
  • @SRaju Edited the answer based on your comment above. – hyde Aug 18 '14 at 17:27
  • Thanks for this answer. Qt documentation says QApplication::exec() doesn't return in some platform. So I am trying to implement the generic solution which helps to figure out the QApplication exit status in all platforms. I am trying to make use of the QCoreApplication::aboutToQuit() signal and connecting it to a slot. Any suggestions for this approach? – SRaju Aug 19 '14 at 00:37