HorizontalHeaderView ColumnWidthProvider confusion
-
According to the documentation
ColumnWidthProvider This property can hold a function that returns the column width for each column in the modelBased on the above documentation, I did the following.
Code snippet below.Not Working - I defined method called columnWidth & gave method name. This did not work.
Working - If I define method directly with property value, then it works. e.g uncomment the following commented code & comment another line.
Window { id : root function columnWidth(column) { console.log(" Value...."+column) return 50 } HorizontalHeaderView { id: horizontalHeader width: parent.width height: 100 // columnWidthProvider: function(column){ // return 50; // } columnWidthProvider : root.columnWidth(column) } }
My conclusion -
So defining function & passing as value will NOT work. It is necessary that method has to inline.Is this assumption true ? Anything missing from my side ?
-
Hello again!
You should inject the column parameter before calling the function
import QtQuick import QtQuick.Controls Window { id: root function columnWidth(column) { if (!horizontalHeader.isColumnLoaded(column)) return -1 console.log(" Value...." + column) return 50 } HorizontalHeaderView { id: horizontalHeader width: parent.width height: 100 model: 3 columnWidthProvider: column => root.columnWidth(column) } }
-
Thanks @afalsa. Yes need to inject the parameter. Documentation is not clear about this. Additional information like this will help.
-
@dheerendra said in HorizontalHeaderView ColumnWidthProvider confusion:
ColumnWidthProvider This property can hold a function that returns the column width for each column in the model
Based on the above documentation, I did the following:
[...]
columnWidthProvider: root.columnWidth(column)
When you are doing this you are not passing a function to the
columnWidthProvider
. This code instead tries to execute theroot.colummWidth
function by passingcolumn
as the parameter (most likelyundefined
here and pass the return value tocolumnWidthProvider
. Did this produces a warning?What you wanted to do is:
columnWidthProvider: root.columnWidth
(or what afalsa wrote).I agree that an example or 2 in the documentation won't hurt.
-