PDA

View Full Version : Level of an item QStandardItemModel for tree



mqt
3rd October 2013, 09:39
I am using QStandardItemModel in a QTreeView and trying to connect the selectionChanged signal of SelectionModel to track the user selection. I was able get the row/column of the selection. How do I get the level of the selected Item?
For example,

Animal
-FourLegged
--Dog
--Cat
-TwoLegged
--Human
Bird
-Flying
--Parrot
-NonFlying
--Chicken

Selecting Animal/Bird should return me 0.
Selecting FourLegged/TwoLegged/Flying/NonFlying should return me 1
Selecting Cat/Dog/Human/Parrot/Chicken should return me 2

Added after 24 minutes:

Currently I solved it by iteratively checking if the parent is NULL. Is there a better way ?

anda_skoa
3rd October 2013, 10:21
Following the parent chain is the only generic way.
Of course, since you control the data of the model, you could store the depth for each entry using a custom role.

Cheers,
_