Reputation: 550
I need to set to none a highlight color of hover and selected item in QTreeWidget. But also I want selected or highlighted items to be with some sort of border.
Now I have my QTreeWidget like this:
Item "model_1" has green background color like "model_2". When I select or just hover it becomes blue and I can not see an original background color. I want to see original background color (in this example it is green).
I've tried to do it with 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);
}
I tried to set background-color: inherit;
but it does not work...
As I saw here:
Note: The RGB colors allowed are the same as those allowed with CSS 2.1, as listed here.
some keywords like "inherit" are not supported..
I need to do it on Linux and Windows.
Please help.
Upvotes: 1
Views: 3014
Reputation: 550
I solved it.
self.setStyleSheet(""" QTreeView::item:selected { } QTreeView::item:hover { } QTreeView::item:hover:selected { } """)
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)
QTreeWidget
objectcustom_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: I'm sorry for such bad style of this answer, I could not understand how to set code style for my code snippets.. It did not work.
Upvotes: 2