4

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

demonplus
  • 5,613
  • 12
  • 49
  • 68
htzfun
  • 1,231
  • 10
  • 41

2 Answers2

6

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();
}
demonplus
  • 5,613
  • 12
  • 49
  • 68
1

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);