繁体   English   中英

从QTreeView中的QModelIndex获取平行索引

[英]Get flat row index from QModelIndex in QTreeView

我有一个分层的Qtreeview结构视图和模型,包含几个带子项的行,方式如下:

                                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                  

每个索引的index.row()都会给出相对于它所在的父级的行号。 是否有任何快速方法可以在Qt本身的图中找到所需的平坦指数? 以上示例显示了非常简单的层次结构。 它非常复杂,即具有多个层次结构和行数。

您可以使用以下代码获取与给定模型索引对应的平面索引:

// 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);
}

希望很清楚它是如何计算指数的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM