How to properly close a widget
-
Hi to everyone,
I'm developing an application that has several custom widgets dynamically added/removed and I was wondering which is the proper way to close them, keeping in mind that they contain data that should be saved. I read something in the Qt's documentation but I can't comprehend if I should define a slot e.g. onClose() in each widget connected to QWidget::close() or if I should override QWidget::closeEvent(). -
@Daniel_Contro said in How to properly close a widget:
override QWidget::closeEvent()
This is one possible way.
Depends on what
QWidget
you have and what data you are editing, you could make use ofwindowModified
(click) -
It's mostly text, but also the widgets order is important to me (it's a hierarchical data structure); I have my own Controller and Model where I should store the data.
Should I override closeEvent() for every different custom widget that requires saving his data then?
Could this be a reasonable wait to override the method?
MyWidget() { connect(this, SIGNAL(closingMyWidget(int)), controller, SLOT(onClosingMyWidget(int))); } ... void MyWidget::closeEvent(QCloseEvent*) { emit closingMyWidget(_myWidgetId); }
And what about all the subwidgets (base classes) created on the heap? Should I destroy them manually or does Qt takes care of them?
-
@Daniel_Contro said in How to properly close a widget:
define a slot e.g. onClose() in each widget connected to QWidget::close()
Just want to say that you cannot connect a slot to another slot / non-signal function...
-
@Daniel_Contro said in How to properly close a widget:
Should I destroy them manually or does Qt takes care of them
If they have a parent, Qt takes care and they get cleaned up together with their parent. If you allocate member vars on heap, you need to take care of them (d'tor).
https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects@Daniel_Contro said in How to properly close a widget:
Could this be a reasonable wait to override the method?
Yes.
-
Hi,
How are these widget managed from a data saving point of view ?