QTreeView合并一些单元格

Iva*_*van 7 c++ model-view-controller qt qtreeview

我有一个自定义QAbstractItemModel和自定义树视图。
是否可以合并QTreeView中的某些单元格?

它看起来像这样:

Num | Name      | Qty | .... |
----|-----------|-----|------|
1   | Unit one  |  5  | .... |
1.1 | Sub unit1 |  3  | .... |
1.2 | Very very big string   |
1.3 | Sub unit2 |  2  | .... |
Run Code Online (Sandbox Code Playgroud)

同样,QTreeWidget :: setFirstColumnSpanned()也不是必须的。

ama*_*ang 5

这是我的第一次尝试,它有效:

void YourClassDerivedFromQTreeView::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if (ThisIsTheBigOne)
    {
        QStyleOptionViewItem opt = option;

        if (selectionModel()->isSelected(index)) {
            opt.state |= QStyle::State_Selected;
        }

        int firstSection = header()->logicalIndex(0);
        int lastSection = header()->logicalIndex(header()->count() - 1);
        int left = header()->sectionViewportPosition(firstSection);
        int right = header()->sectionViewportPosition(lastSection) + header()->sectionSize(lastSection);
        int indent = LevelOfThisItem * indentation();

        left += indent;

        opt.rect.setX(left);
        opt.rect.setWidth(right - left);

        itemDelegate(index)->paint(painter, opt, index);
    }
    else {
        QTreeView::drawRow(painter, option, index);
    }
}
Run Code Online (Sandbox Code Playgroud)

不使用委托类。只是自定义QTreeView的drawRow函数,如果是大的,做一些数学运算并调用itemDelegate(index)->paint,这是QTreeView的默认行为,它对样式表友好。

  • 如何绘制展开/折叠箭头? (3认同)

小智 -1

我会在这里选择一个自定义代表。要么针对整,要么只针对列