How to delete all widgets from QStackedWidget?

6.1k Views Asked by At

How to delete all widgets from QStackedWidget? I need simply remove all widgets from QStackedWidget.

2

There are 2 best solutions below

1
On BEST ANSWER

You need to iterate through all widgets and remove each of them:

for(int i = stackedWidget->count(); i >= 0; i--)
{
    QWidget* widget = stackedWidget->widget(i);
    stackedWidget->removeWidget(widget);
    widget->deleteLater();
}
0
On

I tried this example in Python As a result of 5 pages, only 3 were deleted.

I did some checks and as result come up with an understanding, in a loop page with index 0 needs to be deleted all the time, because then you delete page 0, page 1 becomes 0

    pages = self.ui.stackedWidget_2.count()
    for i in range(pages):
        widget = self.ui.stackedWidget_2.widget(0);
        self.ui.stackedWidget_2.removeWidget(widget);