我有自定义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 | .... |
此外,QTreeWidget :: setFirstColumnSpanned()不是必需的。
答案 0 :(得分:4)
这是我的第一次尝试,它有效:
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);
}
}
不使用委托类。只是自定义QTreeView的drawRow函数,如果它是大的,做一些数学并调用itemDelegate(index) - > paint,这是QTreeView的默认行为,它对样式表很友好。
答案 1 :(得分:0)