Dynamic size of QTreeWidget in PyQt5

我的梦境 提交于 2021-02-20 04:12:36

问题


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.

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

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.

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"
    }

回答1:


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.

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

Example:

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




回答2:


I found a solution that addresses both points (Smallest possible size, and dynamic size change)

It requires a bit of work, some excluded from the simplified code i have in the example. But in essence, i have a function which checks the subcheckboxes for exclusivity. So i use the setData function to define if it's a parent checkbox, or a child checkbox. (subcheckbox) I'm sure there's an easier way, but i'm new at this...

Using the exclusivity function, i checks if it's parent or child. If it's a child, it runs through them, and gets the amount of parents and children. Then i just use each value and multiply with the size of a parent/child item. And then set the heigth from that. If a parent is collapsed, then it's cildren are not included in the calculation of size. Then i simply recalculate the size after each parent item changes.

This allows for smallest possible size, as well as dynamic resizing upon expansion/collapse.



来源:https://stackoverflow.com/questions/45123466/dynamic-size-of-qtreewidget-in-pyqt5

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