我正在使用带有自定义委托的 QTreeView,并且我将 QAbstractItemModel 子类化。我想看到没有孩子的项目的网格线。
在第一次显示时,一切看起来都很好,但是每当鼠标悬停在一个项目上时,底线或顶线就会消失并在悬停回到该项目时重新出现。
因为这是我的第一篇文章,所以我似乎无法发布一些图片来显示不需要的效果。
但我猜我的自定义委托或 QTreeView 的样式表有问题:
void ProjetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() > 0 && !index.model()->hasChildren(index))
{
QPen pen;
pen.setColor(QColor(Qt::lightGray));
pen.setWidth(1);
painter->setPen(pen);
painter->drawRect(option.rect);
QStyledItemDelegate::paint(painter,option,index);
}
else QStyledItemDelegate::paint(painter,option,index);
}
使用的样式表是:
QString treeViewStyle =
"QTreeView { show-decoration-selected: 1; }"
"QTreeView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 white); }"
"QTreeView::item:selected:active { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 lightgray); color: black; }"
"QTreeView::item:selected:!active { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 lightgray); color: black;}" ;
有没有人知道如何摆脱这种动态行为并在首次显示时保持初始正确视图?
谢谢。