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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With