Resize TableView column based on contents in a column
-
How can I resize the contents of a TableView column based upon the contents in a column?
My TableView is declared more or less like this in QML:
@
TableView {
...
...
TableViewColumn {
title: "ID#"
role: "id"
width: 96
resizeable: false
}
}
@ -
Hi,
How about using "resizeColumnsToContents":http://qt-project.org/doc/qt-5/qml-qtquick-controls-tableview.html#resizeColumnsToContents-method ?
-
I would like to ensure column width is set to a variable minimum regardless of contents
-
Sorry but I’m not understanding. Do you mean you want to explicitly set the width ? and it should be based upon the contents ? Can you elaborate ?
-
I wish to set width programmatically based upon contents but with a minimum size, the resizeColumnsToContents doesnt cut it for me.
-
AFAIK there's no such property to set. But you could easily do it by iterating over the contents of TableView's particular column of interest and then storing the length of the text as per your condition. This can then be used to set the width of that particular column.
@
tableview.getColumn(indexofcolumn).width = length-of-text*some-multiplication-factor
@ -
How do you get "length-of-text" ?
-
Using JavaScript String length Property.
-
ruysch: It is probably a bad idea to do this though. TableView doesn't know the width of items that are not visible on screen. This is because a model could have millions of data entries, and would have to actually fetch and instantiate every single one of them in order to calculate the minimum width. Obviously this will not scale well, so resizeToContents will only give you the with of items currently in view. Perhaps you could rather solve this by creating a tool tip or open an extended view if the data doesn't fit on the current column width.
-
Jens: To me it appears as if "resizeColumnsToContents" doesnt consider that the column header might actually be larger than the contents, is this true?
-
ruysch: I believe that is a known issue. I think this would be the appropriate task for it: https://bugreports.qt-project.org/browse/QTBUG-36518
-
jens: How would you make certain that resizeColumnsToContents is envoked automagic?