QTreeView resize section to content but keep interactive
-
I'm trying to figure out how to setup/configure a
QTreeView
so that I one or more section can be programmatically resized to fit the content while still giving the user the ability to resize the section interactively afterwards.From the corresponding
QHeaderView
documentation:QHeaderView::ResizeToContents
QHeaderView will automatically resize the section to its optimal size based on the contents of the entire column or row. The size cannot be changed by the user or programmatically.QHeaderView::Interactive
The user can resize the section. The section can also be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize. (See also cascadingSectionResizes.)I'd basically like a mix of the two: First, resize to fit content programmatically and then allow the user to resize interactively. But this doesn't seem to be an option.
One "workaround" might be to usesetDefaultSectionSize()
as mentioned by the documentation ofQHeaderView::Interactive
. However, I couldn't find any clear indications of what the integersize
property would represent. Surely it wouldn't be number of pixels. Most likely some DPI independent unit?
In any case, this would force me to manually calculate the width needed to fit contents into the corresponding section. In case of text this would require calculating the text size usingQFontMetric
.Is there any better way of achieving what I am after? Am I missing something obvious or is this just a non-standard use-case?
Edit: Seems like
QHeaderView::setDefaultSectionSize()
wouldn't work either because I'd want to assign different sizes for each section. -
Hi @Joel-Bodenmann,
I think you can probably call void QHeaderView::resizeSections(QHeaderView::ResizeMode mode) "manually", for example when the view is shown, and set the resize mode to
interactive
only.