【问题标题】:QTreeWidget custom selection and hover background colors behaviorQTreeWidget 自定义选择和悬停背景颜色行为
【发布时间】:2018-12-12 23:36:00
【问题描述】:

我需要在 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:

注意:允许的 RGB 颜色与 CSS 2.1 允许的颜色相同,如 here 所列。

不支持“继承”等某些关键字..

我需要在 Linux 和 Windows 上进行。

请帮忙。

【问题讨论】:

    标签: css linux windows pyqt qt5


    【解决方案1】:

    我解决了。

    1. 将样式表添加到您的应用程序以激活选择和悬停
    self.setStyleSheet("""
        QTreeView::item:selected {
        }
    
        QTreeView::item:hover {
        }
    
        QTreeView::item:hover:selected {
        }
    """)
    
    1. 为项目的自定义样式委托创建以下类。
    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)
    
    1. 将您的自定义委托设置为 QTreeWidget 对象

    custom_QStyledItemDelegate = MyStyledItemDelegate() tree_widget.setItemDelegate(custom_QStyledItemDelegate)

    1. 当您为项目设置背景颜色时,还可以设置如下数据

    item.setBackground(0, QtGui.QBrush(QtGui.QColor(0, 255, 150, 100)))

    item.setData(0, QtCore.Qt.BackgroundRole, QtGui.QColor(0, 255, 150, 100)) # 需要在 MyStyledItemDelegate 中绘制选定和悬停的数据

    1. 享受

    PS:我很抱歉这个答案的风格如此糟糕,我无法理解如何为我的代码 sn-ps 设置代码风格。它不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多