9

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.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
Darkgaze
  • 2,280
  • 6
  • 36
  • 59

3 Answers3

11

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();
}
synacker
  • 1,722
  • 13
  • 32
7
// 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);
}
cppguy
  • 3,611
  • 2
  • 21
  • 36
  • Thanks! You do this every time? just when you create it? i don´t get it! sorry.When exactly do you do this?. I create all widgets, i add them... but i didn´t know you could set a policy for every page. – Darkgaze Jan 24 '13 at 16:04
  • setting expanding to the pages, doesn´t work... – Darkgaze Jan 24 '13 at 16:08
  • Whenever the current page changes do this – cppguy Jan 24 '13 at 18:24
  • You need to have a good size policy on the QStackedWidget itself, and I seem to need to call adjustSize() on the QStackedWidget too after changing the page widgets' size policies. – Hamish Moffatt Nov 19 '14 at 04:19
  • Thanks this worked for me. This should be added whenever the page changes, for example in the slot of stackedWidget::onCurrentChanged. – RandomGuy Aug 10 '23 at 16:07
2

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

PSyton
  • 908
  • 11
  • 18