强制 Qt/PyQt/PySide QTabWidget 调整到活动选项卡的大小 [英] Force Qt/PyQt/PySide QTabWidget to resize to active tab

查看:64
本文介绍了强制 Qt/PyQt/PySide QTabWidget 调整到活动选项卡的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTabWidget 可以正常工作,只是它没有像我期望的那样重新调整大小.

I have a QTabWidget working correctly except that it doesn't re-size how I would expect it.

小部件中有 2 个标签.每个都有一个带有小部件的 QVBoxLayout.VBox 也按预期工作.

There are 2 tabs in the widget. Each has a QVBoxLayout with widgets in it. The VBox also works as expected.

问题是第一个选项卡在其布局中的小部件比第二个选项卡多.查看第一个选项卡时,选项卡的大小会适当地包含其中的小部件.但是,在查看第二个选项卡时,选项卡小部件的大小与第一个相同,而不是重新调整为可见小部件的大小.

The issue is that the first tab has more widgets in its layout than the second tab. When viewing the first tab, the tab sizes to appropriately contain the widgets inside of it. However, when viewing the second tab, the tab widget stays the same size of the first, instead of re-sizing to the visible widgets.

这种行为是预期的吗?如果是这样,什么是好的解决方法?我希望标签小部件始终缩放到当前活动的小部件.

Is this behavior expected? If so, what is a good work around? Id like for the tab widget to always scale to the currently active widget.

这是该问题的一个示例.所需的行为是让 Tab B 缩小到适当的大小,这可以通过注释掉下面提到的行来看到.

Here is an example of the issue. The desired behavior is for Tab B to shrink to its proper size, which can be seen by commenting out the line noted below.

#!/bin/python
import sys
from PySide import QtGui, QtCore

class TabExample(QtGui.QWidget):
    def __init__(self):
        super(TabExample, self).__init__()
        self.initUI()

    def initUI(self):
        self.vbox_main = QtGui.QVBoxLayout()

        self.table_blank = QtGui.QTableWidget(10, 4)

        self.tabw = QtGui.QTabWidget()
        self.tab_A = QtGui.QWidget()
        self.vbox_A = QtGui.QVBoxLayout(self.tab_A)

        for i in range(20):
            lab = QtGui.QLabel('label %d' %i)
            self.vbox_A.addWidget(lab)

        self.tab_B = QtGui.QWidget()
        self.vbox_B = QtGui.QVBoxLayout(self.tab_B)

        for i in range(5):
            lab = QtGui.QLabel('labelB %d'%i)
            self.vbox_B.addWidget(lab)

        #COMMENT OUT NEXT LINE TO SEE DESIRED SIZE OF TAB B
        self.tabw.addTab(self.tab_A, 'Tab A')

        self.tabw.addTab(self.tab_B, 'Tab B')

        self.vbox_main.addWidget(self.table_blank, 1)
        self.vbox_main.addWidget(self.tabw)

        self.setLayout(self.vbox_main)
        self.setGeometry(0,0, 400, 600)
        self.move(QtGui.QDesktopWidget().availableGeometry().center() - self.frameGeometry().center())
        self.show()


def main():
    app = QtGui.QApplication(sys.argv)
    tabExample = TabExample()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

推荐答案

这应该可以解决问题;根据哪个选项卡处于活动状态更改大小策略.

This should do the trick; change the size policy depending on which tab is active.

def __init__(self):
    super(Widget, self).__init__()
    self.setupUi(self)
    self.tabWidget_2.currentChanged.connect(self.updateSizes)

def updateSizes(self):
    for i in range(self.tabWidget_2.count()):
        self.tabWidget_2.widget(i).setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)

    current = self.tabWidget_2.currentWidget()
    current.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)

这篇关于强制 Qt/PyQt/PySide QTabWidget 调整到活动选项卡的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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