Search code examples
c++qtqt5qtreeviewqt5.5

Get selected element in QTreeView


I have a QTreeView which e.g. shows following structure (all in same column):

parent 1
 - child 1
 - child 2
 - child 3
parent 2
parent 3

Now I make use of a signal currentChanged() to find out which of the elements is selected. But there I get back the same index value 1 for "parent 1" and "child 1" or 2 for "child 2" and "parent 2" - it seems the index always uses the relative row count. So how can I find out which element is selected?

Thanks!


Solution

  • The model index row and column number are relative to the parent model index. Use the QModelIndex::parent() method to get the parent index.