Resize QStackedWidget to the page which is opened [duplicate]

北战南征 提交于 2019-12-07 04:49:42

问题


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.


回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/14480696/resize-qstackedwidget-to-the-page-which-is-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!