Reliably save the state of a widget before it gets closed or destroyed()



  • I have to save the state of a widget when it gets destroyed. However so far I thought about implementing just QWidget::closeEvent (..) , then I figured out that it doesn't get called for some strange reason. Next I tried to connect the QObject's destroyed signal, while it mentioned in the docs that it is emitted before the object is destroyed I had to figure out that it was actually after that, too late to save it's state. :(( So two questions:

    • If I click on the close button in windows, or close the program gracefully there is a way how Qt closes/removes it's widgets. How exactly does this work and which events/signals are to catch.

    • In a class derived from QWidget how can I do something before it gets closed or removed by it's parent. Destructor is not an option, since it might fail.



  • have you testes protected destroy function of "QWidget":http://doc-snapshot.qt-project.org/4.8/qwidget.html#destroy ? It calls before object will be destroyed



  • no it doesn't



  • QWidget::closeEvent get's called for top-level windows. Typically QDialog or QMainWindow, but also any widget that gets created/shown without a parent. So your widget seems to be a child widget?

    I'd still suggest using the destructor, what do you mean with "Destructor is not an option, since it might fail"? A destructor shouldn't fail...



  • So your widget seems to be a child widget?

    yes.

    Well it writes it's state into an XML file. So what if the file is does not exist anymore? Then I would just loose the state :(



  • [quote author="jc-denton" date="1336657863"]Well it writes it's state into an XML file. So what if the file is does not exist anymore? Then I would just loose the state :([/quote]

    Yeah but how would that change if using any other close-triggered signal/event and not the destructor? After all, If the file doesn't exist anymore, recreate it, if the state of the widget is so important.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.