Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do cleaning up on exit in Qt

Tags:

qt

I'd like to do some house keeping stuff (like writing to a file etc) in a Qt app before the app exits. How can I get to this function (exit or whatever is called) in Qt?

like image 451
smallB Avatar asked Nov 17 '11 10:11

smallB


1 Answers

You need to connect a slot with the clean up code to the QCoreApplication::aboutToQuit() signal.

This allows you to delete QObjects with QObject::deleteLater() and the objects will be deleted as you have not yet left the main application event loop.

If you are using a C library that requires a 'shutdown' call you can normally do that after the return from QCoreApplication::exec().

Example for both techniques:

int main(int,char**) {   QApplication app;   library_init();   QWidget window;   window.show();   QObject::connect(&app, SIGNAL(aboutToQuit()), &window, SLOT(closing()));   const int retval = app.exec();   library_close();   return retval; } 
like image 199
Silas Parker Avatar answered Sep 20 '22 07:09

Silas Parker