QTreeView merges some cells

I have a QAbstractItemModel custom property and a custom tree.
Is it possible to merge some cells in QTreeView?

It looks like this:

Num | Name      | Qty | .... |
----|-----------|-----|------|
1   | Unit one  |  5  | .... |
1.1 | Sub unit1 |  3  | .... |
1.2 | Very very big string   |
1.3 | Sub unit2 |  2  | .... |

In addition, QTreeWidget :: setFirstColumnSpanned () is not required.

+4
source share
2 answers

I would go with a custom delegate here. Either for the whole row , or just column .

+1
source

This is my first attempt and it works:

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

. drawRow QTreeView, , itemDelegate (index) → paint, QTreeView , .

+1

All Articles