Anton
Anton

Reputation: 550

QTreeWidget custom selection and hover background colors behavior

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:

enter image description here

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

Answers (1)

Anton
Anton

Reputation: 550

I solved it.

  1. Add stylesheet to your application for activating selection and hovering
self.setStyleSheet("""
    QTreeView::item:selected {
    }

    QTreeView::item:hover {
    }

    QTreeView::item:hover:selected {
    }
""")
  1. Create following class for custom style delegate for items.
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. Set your custom delegate to QTreeWidget object

custom_QStyledItemDelegate = MyStyledItemDelegate() tree_widget.setItemDelegate(custom_QStyledItemDelegate)

  1. When you set background color for item, also set data like following

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

  1. Enjoy

enter image description here

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

Related Questions