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