Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize QStackedWidget to the page which is opened [duplicate]

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.

like image 701
Darkgaze Avatar asked Jan 23 '13 13:01

Darkgaze


3 Answers

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();
}
like image 107
synacker Avatar answered Nov 16 '22 04:11

synacker


// 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);
}
like image 32
cppguy Avatar answered Nov 16 '22 03:11

cppguy


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

like image 34
PSyton Avatar answered Nov 16 '22 04:11

PSyton