c++qtwidgetdelete-operator

C++ Qt how to properly delete widget


If I created any widget something like QPushButton* btn= new QPushButton("Text",this); then does parent widget completely destroys all children widgets or just removes them from it`s internal list?

Should I just use delete btn; or I need to use btn->deleteLater(); to completely destroy it? What the difference between this functions?


Solution

  • does parent widget completely destroys all children widgets or just removes them from it`s internal list?

    The parent widget will destroy all children widgets. From the
    QWidget::QWidget(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) documentation:

    QWidget -> QAbstractButton -> QPushButton

    If parent is nullptr, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent. The new widget is deleted when its parent is deleted.