PyQt5 中 QTreeWidget 的动态大小 [英] Dynamic size of QTreeWidget in PyQt5

查看:43
本文介绍了PyQt5 中 QTreeWidget 的动态大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTreewidget,我希望它尽可能小,可能只有一个分支.但我希望大小根据扩展或折叠的方式相应地改变.以及从适合小部件填充部分的大小开始.

I got a QTreewidget, which i want to be as small as possible, with probably only one branch. But i want the size to change accordingly to how that expands or collapses. As well as start out with a size that fits the filled part of the widget.

我通过添加 QTreeWidgetItems 来制作 QTreewidget,它将 TreeWidget 作为父级,然后再次制作 QTreeWidgetItems,将上述 QTreeWidgetItem 作为父级.

I make the QTreewidget by adding QTreeWidgetItems, which got the TreeWidget as parent, and then making QTreeWidgetItems again which got the above QTreeWidgetItem as parent.

现在,它从左图开始,但我希望它从右图开始.(这是最小尺寸,当您调整窗口大小时.)

Right now, it starts like left image, but i want it to start like the right one.(which is what the minimum size is, when you resize the window.)

  • 如何让它在开始时调整到最小尺寸?

  • How can i make it resize to the minimum size at start?

另外,我如何调整它的大小,比如说当缩进的复选框消失(隐藏)时我调整它的大小,这样占用的空间更少.现在,最小尺寸仍然是可见的.

Also, how can i resize it, say it i resize it when the indented checkboxes are gone(hidden), so that takes even less space. Right now, the minimum size is still what it is, when those are visible.

奖励:当我更改样式时,最小尺寸变小,并使我获得最小尺寸的滚动条,尽管我不知道我更改了什么,因为它仅在我更改样式表时发生.

Bonus: When i changed the style, the minimum size got smaller and made me get scrollbars at minimumsize, despite not knowing what i changed, as it only happens when i change the stylesheet.

根据要求,这里是代码,这是一大堆乱七八糟的东西,因为它只是在进行中.这是我为制作树而制作的课程.

On request, here's the code, it's a whole lot of mess, as it's just work in progress. Here's the class i made for making the tree.

class paramTree(QTreeWidget):
    def __init__(self, Options: dict):
        super().__init__()
        self.setExpandsOnDoubleClick(False)
        self.setHeaderHidden(True)
        self.setRootIsDecorated(False)

        self.Main = self.makeOption(Options['name'], self, Options['state'],0,Options['tooltip'])

        self.Options=[]
        if Options['options'] is not None:
            print(Options['options'])
            for number, choice in enumerate(Options['options']):
                if Options['Active option'] == number:
                    sub = self.makeOption(choice, self.Main,True,1)
                    sub.setFlags(sub.flags() ^ Qt.ItemIsUserCheckable)
                else:
                    sub = self.makeOption(choice, self.Main,False,1)
                self.Options.append(self.indexFromItem(sub))

    def makeOption(name, parent, checkstate, level=0, tooltip=None):
        mainItem = QTreeWidgetItem(parent,[name])
        if tooltip:
            mainItem.setToolTip(0,tooltip)
        if checkstate:
            mainItem.setCheckState(0, Qt.Checked)
        else:
            mainItem.setCheckState(0, Qt.Unchecked)
        mainItem.setExpanded(True)

        return mainItem

这只是放在上面图片中的小部件中.该类采用一个字典,其中包含名称/选项/工具提示的信息和一个带有名称的列表,最终生成子复选框.这样做了 4 次,所以有 4 个 treewidget.

This is just put in a widget in images above. The class takes a dict, which has info for name/options/tooltip and a list with names, that end up making the sub checkboxes. This is done 4 times, so 4 treewidgets.

此外,这里有一个可以传递给班级的 dict 来制作它.尽管如此,并不是所有的都被使用了.

Also, here's a dict that can be passed to the class, to make it. Though, not all of it is used.

optiondict={
        "Active option": 1,
        "Command": "",
        "dependency": None,
        "name": "Convert to audio",
        "options": [
            "mp3",
            "aac",
            "flac"
        ],
        "state": True,
        "tooltip": "Text here"
    }

推荐答案

Layouts 设置尺寸,他们通过widgets的sizeHint()函数取推荐值,但是这个尺寸不匹配你想要什么.

Layouts set the sizes, they take the recommended value through the sizeHint() function of the widgets, but this size does not match what you want.

在您的情况下,您只想降低小部件的高度,我们必须使用 minimumHeight() 设置最小高度,而不是我们借助推荐值设置它的宽度函数 sizeHint().

As in your case you only want to reduce the height of the widget, we must set the minimum height with minimumHeight(), instead the width we set it with the recommended value with the help of the function sizeHint().

self.resize(self.sizeHint().width(), self.minimumHeight())

示例:

class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QVBoxLayout())
        optiondict={
        "Active option": 1,
        "Command": "",
        "dependency": None,
        "name": "Convert to audio",
        "options": [
        "mp3",
        "aac",
        "flac"
        ],
        "state": True,
        "tooltip": "Text here"
        }
        for i in range(4):
            w = paramTree(optiondict)
            self.layout().addWidget(w)
        self.resize(self.sizeHint().width(), self.minimumHeight())

这篇关于PyQt5 中 QTreeWidget 的动态大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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