QTabWidget的大小取决于当前选项卡 [英] QTabWidget size depending on current Tab

查看:1381
本文介绍了QTabWidget的大小取决于当前选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QTabWidget,其中包含不同高度的小部件(其宽度是固定的),但是,QTabWidget的默认实现将最大的小部件的高度选择为自己的高度.

I've a QTabWidget, which contains widgets of different heights (their widths are fixed), however, the default implementation ofQTabWidget selects the biggest widget's height as own height.

我想知道是否存在(可能快速的)方式来根据其当前选项卡更改QTabWidget的大小,以在显示较小的选项卡时节省空间.

What I would like to know if there's a (possible fast) way to change the size of QTabWidget depending on its current tab, to save space when smaller tabs are shown.

推荐答案

您可以将显示的窗口小部件的大小策略设置为QSizePolicy::Preferred,将其他窗口小部件的大小策略设置为QSizePolicy::Ignored.之后,调用adjustSize来更新大小.例如,您可以将QTabWidgetcurrentChanged信号连接到插槽:

You can set the size policy of the widget that is displayed to QSizePolicy::Preferred and the other ones to QSizePolicy::Ignored. After that call adjustSize to update the sizes. For example you can connect the currentChanged signal of the QTabWidget to a slot :

connect(ui->tabWidget,SIGNAL(currentChanged(int)),this, SLOT(updateSizes(int)));

并在插槽中设置尺寸策略并相应地调整尺寸:

And set size policies and adjust sizes accordingly in the slot :

void MainWindow::updateSizes(int index)
{
    for(int i=0;i<ui->tabWidget->count();i++)
        if(i!=index)
            ui->tabWidget->widget(i)->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

    ui->tabWidget->widget(index)->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    ui->tabWidget->widget(index)->resize(ui->tabWidget->widget(index)->minimumSizeHint());
    ui->tabWidget->widget(index)->adjustSize();
    resize(minimumSizeHint());
    adjustSize();
}

这篇关于QTabWidget的大小取决于当前选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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