Search code examples
qtlayoutqwidget

delete qwidget from layout QT


I have a qwidget (we'll call it qwidget1) inside of a layout of an other qwidget (we'll call it qwidget2), I want to delete everything that is in the layout of qwidget2: I would like to clear the layout so there's nothing in it anymore .. what I can do so far is remove completely the qwidget2 by doing:

void QCell::deleteMyChildren(){
   delete this;
}

but it removes the qwidget2 itself.. that's not what I want. Please help me remove the items that are inside the layout.


Solution

  • just loop inside the items in the layout and remove item from layout, then delete item :

    void QCell::deleteMyChildren() {
        while (count() > 0) {
            QLayoutItem * item = takeAt(0);
            delete item;
        }
    }