Get the index of an item from QTreeView
-
Hi,
I have aQTreeViewthat is working fine. Separate from this view, I have anotherQRegLineEditwidget where users can search using a full hierarchical path (liketop.U1ortop.U3.U3_1ortop.U4.U4_1_1) and I display properties to that path. It is also working fine because both are independent of each other which meansQRegLineEditdoesn't depend on any data fromQTreeModel.However, I now want to connect
QRegLineEditandQTreeView. Whenever a user enters a path, I perform some basic checks on whether the input is right or wrong and then I would like to highlight or scroll to that hierarchy/parent in theQTreeView.How do I do that reverse lookup? If I have the index of the item in the
QTreethen I can do thescrollTomethod to go to that item. I just don't know how to get the index from a string.It is to be noted that I cannot simply search for a child say
U4_U4_1_1because it can exist under different parents (duplicates possible) but the full hierarchical path is unique.top -- U1 -- U2 -- U3 -- U3_1 -- U3_2 -- U4 -- U4_1 -- U4_1_1 -- U4_1_2 -- U5 -
One way is to keep a lookup table/map where the path is the key, and the pointer to the tree leaf/node is the data.
-
QAbstractItemModel::match() is worth a look.
-
Hi,
I have aQTreeViewthat is working fine. Separate from this view, I have anotherQRegLineEditwidget where users can search using a full hierarchical path (liketop.U1ortop.U3.U3_1ortop.U4.U4_1_1) and I display properties to that path. It is also working fine because both are independent of each other which meansQRegLineEditdoesn't depend on any data fromQTreeModel.However, I now want to connect
QRegLineEditandQTreeView. Whenever a user enters a path, I perform some basic checks on whether the input is right or wrong and then I would like to highlight or scroll to that hierarchy/parent in theQTreeView.How do I do that reverse lookup? If I have the index of the item in the
QTreethen I can do thescrollTomethod to go to that item. I just don't know how to get the index from a string.It is to be noted that I cannot simply search for a child say
U4_U4_1_1because it can exist under different parents (duplicates possible) but the full hierarchical path is unique.top -- U1 -- U2 -- U3 -- U3_1 -- U3_2 -- U4 -- U4_1 -- U4_1_1 -- U4_1_2 -- U5@vijaychsk
You could keep a "lookup table/map" as @Kent-Dorfman says. But potentially that could be "costly" to maintain, especially if the tree is large/deep.There are two ways you can do it just from the information already held in the tree model.
-
Do like
QFileSystemModeldoes it. It defines a roleQFileSystemModel::FilePathRole Qt::UserRole + 1. That role returns the full file path from each node/leaf. You can then search for this full path (e.g. via @jeremy_k'sQAbstractItemModel::match()). This is simple but "expensive": the tree does a lot of looking back up at parentage (unless you do some extra work) to generate each item's full path to compare. -
Just look down the tree for each element in turn from the desired path. For example, if looking for
/a/b/c, first findaat top-level, thenbamong its children, and finallycamongb's children. You could do that easily yourself from model's nodes' children, or maybe fromQAbstractItemModel::match()without theQt::MatchRecursiveflag being set inQt::MatchFlags flags.
-