user2695082
user2695082

Reputation: 329

Get flat row index from QModelIndex in QTreeView

I have a hierarchical Qtreeview structure view and model containing several rows with children in the following manner:

                                Index(index.row())        Desired flat index

- Row1                           0                               0
  - child1                       0                               1               
  - child2                       1                               2               
  - child3                       2                               3               
    - child11                    0                               4                                        
    - child22                    1                               5                  
      - child 221                0                               6                     
    - child33                    2                               7                  
      - child 31                 0                               8                     
      - child 32                 1                               9                    
  - child4                       3                               10                                      
    - child41                    0                               11                  
    - child42                    1                               12                  
- Row2                           2                               13               
  - child1                       0                               14               
    - child 11                   0                               15                   
    - child 12                   1                               16                  
      - child 121                0                               17                     
  - child2                       1                               18               
  - child3                       2                               19                   
  - child4                       3                               20               
    - child41                    0                               21                  
    - child42                    1                               22                  

Every index's index.row() will give row number with respect to the parent under which it is there. Is there any quick method to find the desired flat index shown above in the figure in Qt itself? The above sample shows a very simple hierarchy. It is very complex i.e. has several hierarchies and number of rows.

Upvotes: 3

Views: 1884

Answers (1)

vahancho
vahancho

Reputation: 21220

You can use the following code to get flat index that corresponds to the given model index:

// Returns the total number of children (and grand children).
static int childCount(const QModelIndex &index)
{
  auto model = index.model();
  int count = model->rowCount(index);
  for (int r = 0; r < count; ++r) {
    count += childCount(index.child(r, 0));
  }
  return count;
}

// Returns the flat index for the given model index (hierarchical).
static int flatIndex(const QModelIndex &index)
{
  if (!index.isValid()) {
    return -1;
  }

  int result = index.row() + 1;
  auto parent = index.parent();

  // Get the number of all items above.
  for (int r = 0; r < index.row(); ++r) {
    result += childCount(index.model()->index(r, 0, parent));
  }

  return result + flatIndex(parent);
}

Hopefully it's clear how it calculates the index.

Upvotes: 1

Related Questions