I have a QTreeWidget with two columns and I'm trying to set the second column width to a very small value.
If I set the width to over 35 it will resize correctly. However if I try to set the width below 35 it will always end up at 35.
Interesting note: Different styles seem to give a different "floor" value. Fusion will go no thinner than 35 and Win will go no thinner than 39.
Here is some sample code that will create a QTreeWidget, add some items and then set and print the second column width a few times.
Here is the output (at least on my end):
This value should be 100: 100
This value should be 35: 35
This value should be 5: 35
This value should be 10: 35
The desired output would be:
This value should be 100: 100
This value should be 35: 35
This value should be 5: 5
This value should be 10: 10
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
import sys
class Window(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setLayout(QVBoxLayout())
self.tree = QTreeWidget()
self.tree.setHeaderLabels(['header1', ''])
self.tree.header().setStretchLastSection(False)
self.tree.header().setSectionResizeMode(0, QHeaderView.Stretch)
self.tree.header().setSectionResizeMode(1, QHeaderView.Fixed)
for i in range(5):
item = QTreeWidgetItem(['test', '_'])
self.tree.addTopLevelItem(item)
self.layout().addWidget(self.tree)
self.tree.header().resizeSection(1, 100) # Setting Size Here
print('This value should be 100: ',self.tree.header().sectionSize(1)) # Printing Size Here
self.tree.header().resizeSection(1, 35) # Setting Size Here
print('This value should be 35: ',self.tree.header().sectionSize(1)) # Printing Size Here
self.tree.header().resizeSection(1, 5) # Setting Size Here
print('This value should be 5: ',self.tree.header().sectionSize(1)) # Printing Size Here
self.tree.header().resizeSection(1, 10) # Setting Size Here
print('This value should be 10: ',self.tree.header().sectionSize(1)) # Printing Size Here
self.show()
app = QApplication(sys.argv)
app.setStyle(QStyleFactory.create('fusion'))
window = Window()
sys.exit(app.exec_())
Just to clarify, I'm not attempting to set the column width multiple times. This code does that in order to demonstrate the various outcomes from setting the width to values above or below 35. My desire is simply to be able to set the column width to any value less than 35.
The lower bound of the section size in a header view is restricted by the minimum section size. The default value of this property is calculated from the current font, style and global strut. However, it can be set programmatically, and the value will be honoured by all the resize modes.
So after changing your example to look like this:
class Window(QDialog):
def __init__(self, parent=None):
...
self.layout().addWidget(self.tree)
print('default minimum size:', self.tree.header().minimumSectionSize())
self.tree.header().setMinimumSectionSize(0)
...
You should see output like this:
default minimum size: 35
This value should be 100: 100
This value should be 35: 35
This value should be 5: 5
This value should be 10: 10
The default minimum size can be restored by setting it to -1
.
NB: resizeSection
will only work when the resize mode is Interactive
or Fixed
. The other modes do not allow the section sizes to be set programmatically.