PDA

View Full Version : How to confirm the clean exit of QApplication



sraju
15th August 2014, 22:57
Dear experts,


I am working on one project where I use Qt for GUI development. Qt is running at the main thread. I would like to confirm the clean exit of the QApplication. This is required to do some post application exit operation such as running exit handlers.

When the application needs to exited I use,

qApp->quit();

to confirm the successful exit of the QApplication, I use

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?

wysota
16th August 2014, 06:56
You can cleanup after QApplication::exec() returns before returning from main().


int main(...) {
QApplication app(...);

int ret = app.exec();
doCleanup();
return ret;
}

Lesiok
16th August 2014, 11:02
How about QCoreApplication::aboutToQuit signal ?

anda_skoa
16th August 2014, 11:57
I guess it mosly depends on whether you need the event loop during cleanup.
If you do, Lesiok's suggestion would give you that, if you don't, I'd also go for Wysota's suggestion.

Cheers,
_

sraju
18th August 2014, 17:49
Thanks for the response.

I want to do the cleanup activities post the QApplication exit. The QApplication documentation says that
"We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms the QApplication::exec() call may not return." QApplication.

Can
void qAddPostRoutine(QtCleanUpFunction ptr) helps to confirm the clean exit of the application and do the cleanup activities?

Added after 19 minutes:

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

anda_skoa
18th August 2014, 17:57
Well, if you happen to be on a platform where exec() does not return, then main() doesn't exit either, so the QApplication does not go out of scope, thus is not deleted, thus its destructor is not invoked and cannot call then post routines.

Cheers,
_

sraju
18th August 2014, 22:45
@ands_skoa Yes you are right. Post routines cannot be executed if QApplication::exec() doesn't return. The platform in which I am working returns from exec.

As a sloution, I am thinking to use QCoreApplication::exit(withSuccessExitCode) ( instead of qApp->quit()) to ensure the successful exit of the QApplication so that I can happily perform post routine activities.

Kindly post your opinion.

wysota
19th August 2014, 07:23
@ands_skoa Yes you are right. Post routines cannot be executed if QApplication::exec() doesn't return. The platform in which I am working returns from exec.

As a sloution, I am thinking to use QCoreApplication::exit(withSuccessExitCode) ( instead of qApp->quit()) to ensure the successful exit of the QApplication so that I can happily perform post routine activities.


Basically calling quit() or exit() are equivalent. The docs of quit() say:


Tells the application to exit with return code 0 (success). Equivalent to calling QCoreApplication::exit(0).

I think you are spending much too much time on thinking about this. It seems that whatever solution you choose that launches after event processing stops will be ok for you. There is no "best" approach here so why spend so much time trying to discover what the "best" solution is?