[SOLVED]Should i Manully delete controls in the MainWindow's destructor?



  • 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!



  • @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.



  • 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



  • 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



  • @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.



  • 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?



  • 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


Log in to reply
 

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