The right way to do something before closing the program - closeEvent, aboutToQuit or the Destructor
-
Hi,
I need to save an int before the application closes. I have been reading about it and I found out that this can be done in a few different ways and I'm wondering what would be the best way to accomplish what I want.
Do I use the QDialog::closeEvent, the aboutToQuit signal or I simply add my code to the destructor?
What would be the main difference between these three different methods?
I again, all I want to do is to be able to save an int that will be extracted from QLabel right before the application exits.
Thanks a lot
-
It depends ;-)
QCoreApplication::aboutToQuit will be emitted when your application is about to quit, so this happens exactly once. The closeEvent of your QDialog is called when your dialog closes. It can happen many times, because the same dialog may be shown many times while the application is running. Finally, the destructor of your QDialog is executed when the dialog object gets deleted. Depending on how your application is designed, the destructor may be called many times (for example when your create a new instance of the QDialog every time you show it, and you delete that instance after the dialog was closed). If you create only a single instance of your QDialog (and maybe show that one instance many times), the destructor is only executed once.
Personally, I would keep the destructor for "clean up" tasks that correspond to the constructor, e.g. the destructor is the correct place for deleting instances you have created in the constructor. I'd tend to save the data when the dialog is closed, in closeEvent() or maybe even use the QDialog::accept() signal - so it gets only saved when the user clicked the "okay" button.
-
Thanks a lot for the good explanation. I will go with the closeEvent().
Thanks