Question

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.

Was it helpful?

Solution

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

OTHER TIPS

// make the stacked widget size to the current page only
for (int i = 0; i < m_ui->stackedWidget->count (); ++i)
{
    // determine the vertical size policy
    QSizePolicy::Policy policy = QSizePolicy::Ignored;
    if (i == m_ui->stackedWidget->currentIndex ())
        policy = QSizePolicy::Expanding;

    // update the size policy
    QWidget* pPage = m_ui->stackedWidget->widget (i);
    pPage->setSizePolicy (policy, policy);
}

You can wrap pages with single button into QFrame with vertical layout and add spacer at bottom.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top