将 QStackedWidget 调整为打开的页面 [英] Resize QStackedWidget to the page which is opened

查看:103
本文介绍了将 QStackedWidget 调整为打开的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 QStackedWidget 调整到打开的页面的大小.

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.

如何使我的 QStackedWidget 具有正在查看的页面的大小.

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.

推荐答案

您可以使用这个 Resizable StackedWidget

在我的实践中,我记得我有从 QStackedWidget 派生的类,并带有重新定义的 addWidget 方法.在这种方法中,我接下来做了:

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

并使用此插槽连接到 currentChange(QWidget*) 信号:

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

这篇关于将 QStackedWidget 调整为打开的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆