Does a QWidget remove itself from the GUI on destruction?

前端 未结 2 2015
忘了有多久
忘了有多久 2021-02-20 15:31

If I delete a QWidget using delete, does it unregister itself from the GUI or do I have to do this manually? Is there a logical reason for this behavio

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-20 15:50

    As already pointed out by @CatPlusPlus , Qt uses an ownership system. So whenver you add widgets to layouts or layouts to widgets and so forth the ownership of the addee is given to the adder/parent. This is usually documented in the method documentation. For example if you look at documentation for QWidget::addLayout(QLayout*), it says the Qwidget takes ownership of the QLayout. When you delete the parent it deletes all its children as well. Read this article for more info .

    Object Trees and Ownership in Qt

    This method is very useful, because in traditional C++ the developer has to keep track of every bit of memory allocated on the heap. This ownership system however requires that the developer onl keep track of the parents.

提交回复
热议问题