Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an already existing layout on a widget?

Tags:

qt

qt4

You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.

from http://doc.qt.io/qt-5.9/qwidget.html#setLayout

Which function is used for deleting the previous layout?

like image 426
Aquarius_Girl Avatar asked Sep 23 '11 12:09

Aquarius_Girl


1 Answers

Chris Wilson's answer is correct, but I've found the layout does not delete sublayouts and qwidgets beneath it. It's best to do it manually if you have complicated layouts or you might have a memory leak.

QLayout * layout = new QWhateverLayout();

// ... create complicated layout ...

// completely delete layout and sublayouts
QLayoutItem * item;
QLayout * sublayout;
QWidget * widget;
while ((item = layout->takeAt(0))) {
    if ((sublayout = item->layout()) != 0) {/* do the same for sublayout*/}
    else if ((widget = item->widget()) != 0) {widget->hide(); delete widget;}
    else {delete item;}
}

// then finally
delete layout;
like image 83
perden Avatar answered Sep 19 '22 16:09

perden