更改 QTreeView 的哪一列显示 expand/collapse 图标

Change what column of QTreeView displays expand/collapse icon

Qt 版本 4.8.4

我有一个反映 QAbstractItemModel 派生模型的 QTreeView。该模型提供多列数据。视图行的 expand/collapse 图标始终显示在逻辑索引为零的列中的单元格中,即使该列已被移动以使其具有非零的可视索引。

QTreeView 是否必须始终在逻辑列零中绘制 expand/collapse 图标?可以通过 QTreeView 或模型对其进行自定义吗?

expand/collapse 图标以及所有其他树分支线确实被迫始终在逻辑列零中绘制。在 Qt/4.8.4/src/gui/itemviews/qtreeview.cpp 中,在 QTreeView::drawRow() 方法中,如果 headerSection(包含逻辑索引),它被硬编码为仅调用 QTreeView::drawBranches()列的)为零。

void QTreeView::drawRow( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
  ...

  for ( int currentLogicalSection = 0; currentLogicalSection < logicalIndices.count(); ++currentLogicalSection )
  {
    int headerSection = logicalIndices.at(currentLogicalSection);

    ...

    // If the zeroeth logical column, ...
    if ( headerSection == 0 )
    {
      ...

      // ... draw tree branches and stuff.
      drawBranches(painter, branches, index);
    }
    else
    {
      ... 
    }

    ...
  }

  ...
}