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?
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?
You just use
delete layout;
like you would with any other pointer you created using new
.
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;
You just use
delete layout;
like you would with any other pointer you created using new
.
This code deletes the layout, all its children and everything inside the layout 'disappears'.
qDeleteAll(yourWidget->findChildren<QWidget *>(QString(), Qt::FindDirectChildrenOnly));
delete layout();
This deletes all direct widgets of the widget yourWidget
. Using Qt::FindDirectChildrenOnly
is essential as it prevents the deletion of widgets that are children of widgets that are also in the list and probably already deleted by the loop inside qDeleteAll
.
Here is the description of qDeleteAll
:
void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Deletes all the items in the range [begin, end] using the C++ delete > operator. The item type must be a pointer type (for example, QWidget *).
Note that qDeleteAll
needs to be called with a container from that widget (not the layout). And note that qDeleteAll
does NOT delete yourWidget
- just its children.
Now a new layout can be set.
QObject
to yourWidget
unrelated to layout/widget structure and this issue might be hard to find. –
Carbine I want to remove the current layout, replace it with a new layout but keep all widgets managed by the layout. I found that in this case, Chris Wilson's solution does not work well. The layout is not always changed.
This worked for me:
void RemoveLayout (QWidget* widget)
{
QLayout* layout = widget->layout ();
if (layout != 0)
{
QLayoutItem *item;
while ((item = layout->takeAt(0)) != 0)
layout->removeItem (item);
delete layout;
}
}
From Qt6's docs:
The following code fragment shows a safe way to remove all items from a layout:
QLayoutItem *child;
while ((child = layout->takeAt(0)) != nullptr) {
...
delete child->widget(); // delete the widget
delete child; // delete the layout item
}
This assumes takeAt() has been correctly implemented in the QLayout subclass. Follow link for details.
I had the issue today where I was trying to delete a layout of a widget to replace it with another. What happened was that the previous layout never disappeared inspite of the call to delete layout();
.
So I figured out a very simple, effective and supported workaround by taking advantage of the fact that layouts cannot have more than one parent and can be reparented.
I created a deletePreviousLayout
function:
void MyWidget::deletePreviousLayout()
{
QWidget dummyWidget; // On the stack to deallocate it at the end of the function.
dummyWidget.setLayout(layout()); // Reparenting
}
And that's it.
© 2022 - 2024 — McMap. All rights reserved.