我有一个自定义 QAbstractItemModel 和自定义 TreeView 。
是否可以合并 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() 也不是必需的。
最佳答案
这是我的第一次尝试,它成功了:
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)类。只需自定义 QTreeView 的 drawRow 函数,如果它很大,做一些数学运算并调用 itemDelegate(index)->paint,这是 QTreeView 的默认行为,并且它对样式表很友好。
关于c++ - QTreeView 合并一些单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27149733/