Solved How to retrieve items indices from a treeView?
-
Just add the third parameter to the index method:
theModel.index(0,1)
retrieves row 0, column 1 of the root
theModel.index(0,1,theModel.index(0,0))
retrieves row 0, column 1 of the child of the first item in the rootI hope this is clear enough
-
Be aware that TreeView is very limited and has many bugs.
Indexes are unsafe to use. You can find bugreports on qt bugreports.
You may want to support https://bugreports.qt.io/browse/QTBUG-56490?filter=-2 -
@Kofr said in How to retrieve items indices from a treeView?:
Indexes are unsafe to use
Just to clarify: They are 100% safe to use in the C++ side
-
@VRonin so far they are not manipulated by TreeView itself
-
@Kofr I've read your question one year ago, referring to this one
That's why you are saying it has many bugs?
let me clarify why I needed to retrieve the index, I'm trying to access each item on its own so I can apply a specific delegate to it (i.e. the green icon next to Camera connected will turn on if a specific condition is met on the back-end, that's why I tried to address each item on its own.
So far the method VRonin provided worked well and gave me the data I expected, so I am hoping when I finish my code, manipulating the items through the back-end, the delegate will be applied correctly.
I will keep you posted if I encountered any problem, thanks for the heads up.
-
@Haitham the problems begin occures when you make item moves in TreeView. After this operation items are shown incorrectly and you can not catch correct indexes unfortunataly.
-
@VRonin
Thanks so much buddy, your method worked and I understood it, the problem with my method that I wasn't referring to a parent item so I can get its child.What I eventually want to achieve is to let the delegates react to some back-end code, so I want to apply delegates to only a specific item once the condition is met (e.g., if Camera is connected, the green off icon should be replaced with a green on one) and so on. By saying so, I think it makes sense now why I wanted to be able to reach each item through its index.
Please, let me follow it by another question,
is it better to address the items on the C++ side or do it on the QML side? -
So far in my case, the items are all static and only the delegates are updated. I think I should not encounter this problem but we will see
Thanks a lot, once again!
-
@Haitham said in How to retrieve items indices from a treeView?:
What I eventually want to achieve is to let the delegates react to some back-end code, so I want to apply delegates to only a specific item once the condition is met (e.g., if Camera is connected, the green off icon should be replaced with a green on one) and so on. By saying so, I think it makes sense now why I wanted to be able to reach each item through its index.
Just save the state as a separate role in the item and let the delegate check that role before deciding what to do
-
@VRonin
Can you please provide an example or something to follow? Because I am still a newbie at both Qt and QML.
Sorry for bothering you with my many questions.Update:
as you can see in the code, I change the state of the delegate through the mouse area in the delegate (it's commented out in the code). I was using it to test the states, now I've noticed another thing; Whenever I collapse the parent Item and then expand it, the previous states are not saved....does this have to do anything with what you mentioned?