Resize QStackedWidget to the page which is opened

darkgaze picture darkgaze · Jan 23, 2013 · Viewed 8k times · Source

I want my QStackedWidget to resize to the page which is opened.

I got a lot of widgets attached to the first page, but the rest pages have only one button. So they stay so big, and the first page is ok.

How can I make my QStackedWidget to have the size of the page being viewed.

The reason why i want to do this is that I have three different options, and after that I have other things. If I change to the mode where there is a button and then a lot of white space, I don´t see why doesn´t it resize.

Answer

synacker picture synacker · Jan 23, 2013

You can use this Resizable StackedWidget

In my practice i remember that i had class derived from QStackedWidget with redefined addWidget method. In this method i did next:

void ResizableStackedWidget::addWidget(QWidget* pWidget)
{
   pWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
   QStackedWidget::addWidget(pWidget);
}

And connected to currentChange(QWidget*) signal with this slot:

void ResizableStackedWidget::onCurrentChanged(int index)
{
   QWidget* pWidget = widget(index);
   Q_ASSERT(pWidget);
   pWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
   pWidget->adjustSize();
   adjustSize();
}