我需要在 QTreeWidget 中将悬停和选定元素的突出显示颜色设置为无。但我也希望选定或突出显示的元素带有某种边框。
现在我的 QTreeWidget 是这样的:
元素“model_1”与“model_2”一样具有绿色背景色。当我选择或只是悬停时,它变成蓝色,我看不到原始背景颜色。我想查看原始背景颜色(在本例中为绿色)。
我已经尝试用 stylesheet 来做到这一点:
QTreeView::item:selected {
border: black;
border-radius:5px;
background-color: rgba(0,128,255,100);
}
QTreeView::item:hover {
border: black;
border-radius:1px;
background-color: rgba(0,128,255,95);
}
QTreeView::item:hover:selected {
border: black;
border-radius:1px;
background-color: rgba(0,128,255,70);
}
我尝试设置 background-color: inherit;
但它不起作用...
如我所见here :
Note: The RGB colors allowed are the same as those allowed with CSS 2.1, as listed here.
不支持某些关键字,例如“inherit”..
我需要在 Linux 和 Windows 上执行此操作。
请帮忙。
最佳答案
我解决了。
- 将样式表添加到您的应用程序以激活选择和悬停
self.setStyleSheet(""" QTreeView::item:selected { } QTreeView::item:hover { } QTreeView::item:hover:selected { } """)
- 为元素的自定义样式委托(delegate)创建以下类。
class MyStyledItemDelegate(QtWidgets.QStyledItemDelegate): ''' For overriding behavior of selection and hovering in QTreeView and QTreeWidget When you set background color (QtGui.QColor()) to QTreeWidgetItem you also must set this color like: item.setData(0, QtCore.Qt.BackgroundRole, QtGui.QColor()) ''' def paint(self, painter, option, index): def draw_my(option, painter, brush, text, icon): if brush is None: brush = QtGui.QColor(255, 255, 255, 0) # This is original background color. I just set alpha to 0 which means it is transparent x, y = (option.rect.x(), option.rect.y()) h = option.rect.height() painter.save() painter.setFont(option.font) if icon: icon = icon.pixmap(h, h) painter.drawPixmap(QtCore.QRect(x, y, h, h), icon) painter.drawText(option.rect.adjusted(h, 0, 0, 0), QtCore.Qt.AlignLeft, text) else: painter.drawText(option.rect, QtCore.Qt.AlignLeft, text) painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceAtop) painter.setPen(QtGui.QPen(QtCore.Qt.NoPen)) painter.fillRect(option.rect, brush) painter.setBackgroundMode(QtCore.Qt.OpaqueMode) painter.setBackground(brush) painter.drawRect(option.rect) painter.restore() # Also should be activated in StyleSheet # Selected Hovered if (option.state & QtWidgets.QStyle.State_Selected) or (option.state & QtWidgets.QStyle.State_MouseOver): option.font.setWeight(QtGui.QFont.Bold) brush = index.data(QtCore.Qt.BackgroundRole) text = index.data(QtCore.Qt.DisplayRole) icon = index.data(QtCore.Qt.DecorationRole) draw_my(option=option, painter=painter, brush=brush, text=text, icon=icon) else: QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
- 将您的自定义委托(delegate)设置为
QTreeWidget
对象
custom_QStyledItemDelegate = MyStyledItemDelegate() tree_widget.setItemDelegate(custom_QStyledItemDelegate)
- 当你为元素设置背景颜色时,还要设置如下数据
item.setBackground(0, QtGui.QBrush(QtGui.QColor(0, 255, 150, 100)))
item.setData(0, QtCore.Qt.BackgroundRole, QtGui.QColor(0, 255, 150, 100)) # Need for paint selected and hoovered data in MyStyledItemDelegate
- 享受
PS:我很抱歉这个答案的风格如此糟糕,我无法理解如何为我的代码片段设置代码风格。它没有用。
关于css - QTreeWidget 自定义选择和悬停背景颜色行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51175167/