Write about Memory management in Qt ?

Write about Memory management in Qt ?

Asked on December 17, 2018 in Qt.
Add Comment


  • 3 Answer(s)

    To  build the  own hierarchy with QObjects, that  initialise  the newly created  QObjects with a parent:

    QObject* parent = new QObject();
    QObject* child = new QObject(parent);
    

       It is enough to delete the parent, because the parents destructor  to take care the destroying child. ( In even they delete child manually before the parent.)

    And  also to delete the child first, the order does not  matter.

    MyClass is not a child of QObject,  but they  use the plain C++ way of doing things.

        The parent–child hierarchy  of QObject  is generally independent  the hierarchy of the C++ class hierarchy/inheritance tree. To  assigned child   to be a direct subclass of it’s parent. Any (subclass of) QObject will suffice.

         There might be some constraints imposed by the constructors for other reasons, however; such as in QWidget(QWidget* parent=0), the parent must be another QWidget,   but the Qt’s hierarchy system is general,  they allowe to have any QObject as a parent.

    Answered on December 17, 2018.
    Add Comment

    Just solve this coding:

    QVBoxLayout* layout = new QVBoxLayout;
    QPushButton someButton = new QPushButton; // No owner specified.
    layout->addWidget(someButton); // someButton still has no owner.
    QWidget* widget = new QWidget;
    widget->setLayout(layout); // someButton is "re-parented".
                                // widget now owns someButton.
    

    Coading 2:

    QMainWindow* window = new QMainWindow;
    QWidget* widget = new QWidget; //widget has no owner
    window->setCentralWidget(widget); //widget is now owned by window.
    

     

    Answered on December 17, 2018.
    Add Comment

    Using the simplest parent code:

    QObject* parent = new QObject();
    QObject* child = new QObject(parent);
    delete parent;//all the child objects will get deleted when parent is deleted, child object which are deleted before the parent object is removed from the parent's child list so those destructor will not get called once again
    
    Answered on December 17, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.