Get QModelIndex from Treeview rowDelegate
-
Hi everybody
The rowDelegate of treeview only allow me to access styleData.row, which is not enough since i need to draw row base on item's other attribute tooSo, by any chance, can i get it? Thanks :)
-
@p3c0 Hi p3c0, it's good to see you again :)
I tried the index(row, column, parentIndex) already. Indeed ,it returns a QModelIndex of the first level item, but not working with child item. We already talked about this on other topic, without QModelIndex, we can't get the item from treeview.
I also read the post you recommend, but still not finger out how to do it.Am i missing something?
Thanks -
@p3c0 Hi p3c0, it's good to see you again :)
I tried the index(row, column, parentIndex) already. Indeed ,it returns a QModelIndex of the first level item, but not working with child item. We already talked about this on other topic, without QModelIndex, we can't get the item from treeview.
I also read the post you recommend, but still not finger out how to do it.Am i missing something?
Thanks -
@illunara You can make use of a little bit C++ side code in this case. Create a
Q_INVOKABLE
function which will accept the row. Then you can get the children of that particular top level item and then return their index's. -
@p3c0 Yeah, that's the problem. how can i get the parent level item?
With tableview or listview, this can get done easily since it has only one level, treeview is difference :( -
@illunara The only way is to iterate and find children of the top level item and then children of these children and so on.
-
@p3c0 Can we access to the current component of rowDelegate?
Like that, we can create a work-around using itemDelegate? -
@illunara Afaik getting data through model instead of delegate is the recommended way. Didn't it work through model ?
-
@p3c0 The only thing i have frow rowDelegate is row number, i can't get model data just with that (Except on the top level)
@illunara Why not ? For eg. following code gives you
QModelIndex
's of all top level item.var top = []; for (var i = 0; i < myModel.rowCount(); ++i ) { top[i] = myModel.index( i, 0 ); } console.log(top)
Once you get the top level you can get their children. It would be better if you do the same on the C++ side as I think getting children should be lot more easier there.
-
@illunara Why not ? For eg. following code gives you
QModelIndex
's of all top level item.var top = []; for (var i = 0; i < myModel.rowCount(); ++i ) { top[i] = myModel.index( i, 0 ); } console.log(top)
Once you get the top level you can get their children. It would be better if you do the same on the C++ side as I think getting children should be lot more easier there.
@p3c0 Well, because i want to draw the row's color base on item data's attribute. Say :
I have a field in the item which tell me if data had been changed or not, if yes, then set the row's color to blue. But i can't access to its data with only row number of rowDelegateIf i have QModelIndex i can do it normally with
static_cast<Class*>(index.internalPointer()).getData(....)
Here my my rowDelegate code for TableView btw
...rowDelegate : Rectangle { color : (model.get(styleData.row, "DataChanged") ? "#000000" : "#ffffff" }
...
-
@p3c0 Well, because i want to draw the row's color base on item data's attribute. Say :
I have a field in the item which tell me if data had been changed or not, if yes, then set the row's color to blue. But i can't access to its data with only row number of rowDelegateIf i have QModelIndex i can do it normally with
static_cast<Class*>(index.internalPointer()).getData(....)
Here my my rowDelegate code for TableView btw
...rowDelegate : Rectangle { color : (model.get(styleData.row, "DataChanged") ? "#000000" : "#ffffff" }
...
@illunara Try making use of
dataChanged
signal. I hope you do that in model when you update the data. Then on QML side you can useConnections
to listen to the signal keeping the model astarget
. When the signal is emitted the corresponding handler will be invoked which will return the model indexes which were updated. Once you get those indexes getting its value(data) should be easier IMO. I haven't tried it myself but seems workable. -
@illunara Try making use of
dataChanged
signal. I hope you do that in model when you update the data. Then on QML side you can useConnections
to listen to the signal keeping the model astarget
. When the signal is emitted the corresponding handler will be invoked which will return the model indexes which were updated. Once you get those indexes getting its value(data) should be easier IMO. I haven't tried it myself but seems workable.@p3c0 Still, without access to the rowDelegate component, even if i have modelIndex of the item, nothing can be done.
And i think the rowDelegate invoke got invoked before emit dataChanged signal.
Maybe we should left it at here, i will think another way to handle this.
Thank you so much for helping me :) -
@p3c0 Still, without access to the rowDelegate component, even if i have modelIndex of the item, nothing can be done.
And i think the rowDelegate invoke got invoked before emit dataChanged signal.
Maybe we should left it at here, i will think another way to handle this.
Thank you so much for helping me :)@illunara I think it can be done. Once you get the model index you just have to request the new data (since binding to rolename doesn't work). Something like this inside
rowDelegate
:Connections { target: dataModel onDataChanged: { var index = topLeft update(index) } } function update(modelIndex) { var value = dataModel.data(modelIndex, dataModel.getRoleKey("SomeRole")) //SomeRole is the role defined model which reflects the data you require. color = value }
getRoleKey
function is explained here. -
@illunara I think it can be done. Once you get the model index you just have to request the new data (since binding to rolename doesn't work). Something like this inside
rowDelegate
:Connections { target: dataModel onDataChanged: { var index = topLeft update(index) } } function update(modelIndex) { var value = dataModel.data(modelIndex, dataModel.getRoleKey("SomeRole")) //SomeRole is the role defined model which reflects the data you require. color = value }
getRoleKey
function is explained here.@p3c0 Nice idea, but this approach have one problem
Whenever a dataChanged emit, every row will get updated. Which mean, we need data to compare and find out which row, that data belong to.Only thing we have is styleData.row and its not enough for a complicate model. And unfortunately, we can't access to it when call the update() method either. The value is undefined (ofc, I put it inside rowDelegate)
-
@p3c0 Nice idea, but this approach have one problem
Whenever a dataChanged emit, every row will get updated. Which mean, we need data to compare and find out which row, that data belong to.Only thing we have is styleData.row and its not enough for a complicate model. And unfortunately, we can't access to it when call the update() method either. The value is undefined (ofc, I put it inside rowDelegate)
Whenever a dataChanged emit, every row will get updated. Which mean, we need data to compare and find out which row, that data belong to.
Yes. You just need to add a simple condition before calling function
update
var index = topLeft if(index.row===styleData.row) update(index)
-
Whenever a dataChanged emit, every row will get updated. Which mean, we need data to compare and find out which row, that data belong to.
Yes. You just need to add a simple condition before calling function
update
var index = topLeft if(index.row===styleData.row) update(index)
@p3c0
Root
| ---- Item A // Row 1
|#####|---------- Item B // Row2 (Expected Row 1)
|#####|---------- Item C // Row3 (Expected Row 2)
| ---- Item D //Row4 (Exoected Row 2)styleData.row is relative only to rootItem. That's why this method will not work