Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to set the default size of column in QT Tree view with longest member of Column
I need to set the defaut size of Column in QT tree view the longest name member of Column.
How can I get the. I mean how can I get the width of Longest name member of Column in QT tree view.
So I can make the default size of column to that.
Is there any function for that in QT?
Since you write the model you've access to your data and can find out the longest string.
Pl45m4 last edited by Pl45m4
You could try to work with your QTreeViews Header. (
(Call ResizeToContent everytime you add a new branch to your model, so it will always have the max length)
Edit: You dont need to call it, since it's a Resizing Strategy. It should update the size by itself.
The actual "length" of a word depends on font (pixel / point sizes) and letters, so I dont think it will work by counting and comparing single characters. (-> "WWW", 3 letters, needs more space than 4 capital i`s)
@Pl45m4 This is causing performace issue Hence I thought to use set the column to default size
JonB last edited by
If you're not prepared to go via
ResizeToContents, you yourself have to measure every item in the correct font etc. and use that to set a width.
when we apply setSectionResizeMode(Qt::HeaderView::ResizetoContents)
tree view will resize automatically. Is there any way we can disable that after making the call
setSectionResizeMode in constructor
Set the mode you want to use and the call QHeaderView::resizeSections. That should do what you want.
@SGaist can give some code snippet?
A snippet of what ?