[SOLVED]Should i Manully delete controls in the MainWindow's destructor?
-
wrote on 9 Mar 2015, 08:09 last edited by opengpu2 3 Sept 2015, 11:22
i create the ui controls setting it's parent to the window?
so should i delelte and set them to NULL in the window's destructor?
or should i do this delete in the window's closeEvent() func?
thank you! -
wrote on 9 Mar 2015, 08:32 last edited by
@opengpu2 said:
i create the ui controls setting it's parent to the window?
so should i delelte and set them to NULL in the window's destructor?
or should i do this delete in the window's closeEvent() func?
thank you!posted 22 minutes ago
Neither. If the control has the window as its parent, the window will automatically take care of cleaning it up.
-
wrote on 9 Mar 2015, 08:34 last edited by
GENERAL RUE: When an QObject is destroyed, it automatically destroys all its children.
If you did something like
MyClass::MyClass(QWidget* parent): QMainWindow(parent)
{
label = new QLabel(this)
edit = new QLineEdit(this);
....
}it works
-
wrote on 9 Mar 2015, 09:22 last edited by
how about QGraphicsItem ?
i notice that, when pItem was created, no parent was sent to it, and then just scene->addItem(pItem).
what should i do when i close the widget which contain the QGraphicsView/Scene?
should i delete all the QGraphicsItem in the QGraphicsView's destructor or in closeEvent?
thank you -
wrote on 9 Mar 2015, 09:32 last edited by
@opengpu2 said:
how about QGraphicsItem ?
Yes. If you
addItem
, the parent and ownership is set to the QGraphicsView/Scene. The documentation for these classes describes this. I suggest opening up Qt assistant and reading, the documentation is really terrific. -
wrote on 9 Mar 2015, 09:48 last edited by
thank you.
so u mean, like this:
pItem = new QGraphicsItem( 0 ); //no parent was send here
scene->addItem(pItem);the parent is still the scene.
and when i close the widget which owns the QGraphicsView/Scene.
still no need to Manully delete the pItem, right? -
wrote on 9 Mar 2015, 10:17 last edited by
From Qt docs
QGraphicsScene::addItem()
Adds or moves the item and all its children to this scene. This scene takes ownership of the item.So, when a scene is destroyed, all items will be destroyed too
7/7