3

Is there a way to get "visual row" of a given QModelIndex?

Let's say we have a tree with some nodes expanded and some not. For example, take a partially expanded tree and click on a last item:

item1    # (visual) row 1
  item2  # 2
  item5  # 3
item7    # 4
  item8  # 5 <-- click here

I want to know that item I clicked is in the 5th row, programmatically. QModelIndex's rows and columns are not helpful, as there is hierarchy here.

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
Vasaka
  • 1,953
  • 1
  • 19
  • 30
  • possible duplicate of [Easier way to find (visual) position of QModelIndex in QTreeView](http://stackoverflow.com/questions/1739643/easier-way-to-find-visual-position-of-qmodelindex-in-qtreeview) – Marc Mutz - mmutz Jun 24 '12 at 11:37

1 Answers1

4

If all of your rows have the same height, you can compute the visual row like this:

int visual_row(const QTreeView *tv, const QModelIndex &mi)
{
    const QRect visualRect = tv->visualRect(mi);
    if (visualRect.isValid())
        return visualRect.y() / visualRect.height();
    else
        return -1; // invisible
}
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90