SOLVED: Remove widgets from Layout
-
wrote on 30 Jul 2013, 18:54 last edited by
Hello,
How do I remove all widgets from a layout so as to leave it empty
@
QPushButton *button1 = new QPushButton("Button 1");
QPushButton *button2 = new QPushButton("Button 2");
ui->horizontalLayout->addWidget(button1);
ui->horizontalLayout->addWidget(button2);
@How do I empty ui->horizontalLayout?
Thanks in Advance -
wrote on 30 Jul 2013, 19:22 last edited by
I do not know of a method to remove all widgets at once, but if you have
pointers to the widgets, you can remove them from the layout like this:
@
ui->horizontalLayout->removeWidget(button1);
ui->horizontalLayout->removeWidget(button2);
@ -
wrote on 30 Jul 2013, 20:22 last edited by
That would work, But I need to clear all the widgets in the first statement of the function. In otherwords, the widgets declared in the previous call are removed when the function is called. So at that point you can't use removeWidget()
-
Hi,
You're looking for "takeAt":http://qt-project.org/doc/qt-4.8/qlayout.html#takeAt
-
wrote on 31 Jul 2013, 01:20 last edited by
As SGalst suggest, please read the documentation carefully.
-
wrote on 31 Jul 2013, 08:23 last edited by
I am doing it this way normally
@
QLayoutItem *wItem;
if (!ui->widget->layout())
ui->widget->setLayout(new QVBoxLayout());
while ((wItem = ui->widget->layout()->takeAt(0)) != 0)
{
if (wItem->widget())
wItem->widget()->setParent(NULL);
delete wItem;
}
@Of course you can avoid the generation of the layout and the remove everything from the empty layout...
-
wrote on 31 Jul 2013, 16:07 last edited by
Supposing the contents of the Layout are widgets added with layout->addWidget(). How do I remove them? The methods above remove items of type QLayoutItem only
-
wrote on 31 Jul 2013, 16:53 last edited by
That's what the documentation for QLayoutItem says:
bq.
isEmpty() returns whether the layout item is empty. If the concrete item is a QWidget, it can be retrieved using widget(). Similarly for layout() and spacerItem(). -
wrote on 31 Jul 2013, 17:35 last edited by
Thanks butterface, But am just a Qt beginner. Could you please show me how to do it in case its a widget that I want to remove without using removeWidget().
Code
@QLayoutItem *child; //replace with widget
while ((child = ui->HLayout->takeAt(0)) != 0) {
delete child;
}@ -
wrote on 31 Jul 2013, 22:21 last edited by
Without any testing and just the first guess this should look somehow like
@
QWidget *wItem;
while ((wItem = ui->widget->widget()->takeAt(0)) != 0)
{
wItem->setParent(NULL);
delete wItem;
}
@If this does not work please have a look at the documentation and come back here if that does not help.
-
wrote on 1 Aug 2013, 08:15 last edited by
Well, Thanks guys. I ended up using a widget container and did this:
@QList<QWidget *> Widgets = ui->widget->findChildren<QWidget *>();
foreach(QWidget * child, Widgets)
{
delete child;
}@But now there is another problem: "Application crashes when removing tableWidget":http://qt-project.org/forums/viewthread/30727/
-
Hi,
You're looking for "takeAt":http://qt-project.org/doc/qt-4.8/qlayout.html#takeAt