Qt World Summit: Submit your Presentation

Make selection background have similar background to item being selected

  • How can I make the background a blend of current cell background for when an item is selected ?

    The default item-selection background is blue and say if my cell is red, then I'd want it to be red with slightly less opacity:

    Example image of how it is

    Example image of how I'd like it to be

    I have tried setting the color to be transparent:

    setStyleSheet("selection-background-color: transparent")

    And also the rgba feature with opacity 1%:

    setStyleSheet("selection-background-color: rgba(255, 255, 255, 1)")

    But neither retain the original color.

  • Lifetime Qt Champion


    One way would be to use a QStyledItemDelegate where you handle your special painting.

    Hope it helps

  • @SGaist Hello, thank you for the quick response. If you could, can you point me towards some resources on making delegates and how I'd use the paint option (I've not used delegates before). And are you saying that there's no other way of doing this? Thank you

  • Lifetime Qt Champion

    The "extreme" example is the Star Delegate Example and shows the technique to draw the selection rectangle when the item is selected.

  • @SGaist I'll try and make this work for python, thank you

  • @SGaist I've implemented it in python and it seems that it overwrites the background set previously? I have a tablecell item that I've applied a gradient to so it's reddish.

    This is what I'm using:

    class SelectionHighlightDelegate(QtWidgets.QItemDelegate):
    def __init__(self, parent=None, *args):
        QtWidgets.QItemDelegate.__init__(self, parent, *args)
    def paint(self, painter, option, index):
    # set background color
        if option.state & QtWidgets.QStyle.State_Selected:
    # set text color
        value = index.data(QtCore.Qt.DisplayRole)
        if option.state & QtWidgets.QStyle.State_Selected:
    # Left indent
        painter.translate(3, 0)
        painter.drawText(option.rect, QtCore.Qt.AlignLeft, value)

    (Which is a pyqt5 version of this)

    In another class with the main application/window I have some table items with a custom background

  • Lifetime Qt Champion

    You're making the background white no matter the state except for State_Selected, you should then rather use the options values

  • @SGaist I had that part commented out in my code but it still ignores the background set previously to the cell. I have two tables, one I set the custom delegate on and that one has the background colors reset. The other one is fine. What do you mean by options values?

  • Lifetime Qt Champion

    The option parameter gives you all the information about colors to use and the rest for painting so if you set a particular color you should use the information from the palette given by option to do the drawing.

  • @SGaist Argh everything's too confusing. I think I'll just leave it for now haha