Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTreeView merge some cells

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

It shold 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  | .... |

Also, QTreeWidget::setFirstColumnSpanned() do not that is necessary.

like image 533
Ivan Avatar asked Nov 26 '14 12:11

Ivan


1 Answers

This is my first try, 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);
    }
}

Delegate classes are not used. Just custom QTreeView's drawRow function, if it's the big one, do some math and call itemDelegate(index)->paint, this is the default behavior of QTreeView, and it's friendly for stylesheet.

like image 55
amanjiang Avatar answered Nov 19 '22 04:11

amanjiang