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.
-
You could try to work with your QTreeViews Header. (
treeView->header()
)https://doc.qt.io/qt-5/qheaderview.html#ResizeMode-enum
(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)
-
@Ayush-Gupta
If you're not prepared to go viaResizeToContents
, 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 -
Hi,
Set the mode you want to use and the call QHeaderView::resizeSections. That should do what you want.
-
A snippet of what ?