更改QTreeView的哪一列显示展开/折叠图标

时间:2019-07-09 16:29:47

标签: c++ qt qtreeview

Qt版本4.8.4

我有一个QTreeView反映了QAbstractItemModel派生的模型。该模型在多列中提供数据。视图行的展开/折叠图标始终显示在逻辑索引为零的列中的单元格中,即使该列已移动以使其视觉索引非零也是如此。

QTreeView是否被迫总是在逻辑列零中绘制展开/折叠图标?可以通过QTreeView或模型进行自定义吗?

1 个答案:

答案 0 :(得分:0)

展开/折叠图标以及所有其他的树状分支线确实必须始终绘制在逻辑列零中。在Qt / 4.8.4 / src / gui / itemviews / qtreeview.cpp中,在QTreeView::drawRow()方法中,如果QTreeView::drawBranches()(包含逻辑索引)仅调用headerSection的列)为零。

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
    {
      ... 
    }

    ...
  }

  ...
}