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 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.
-
Hi,
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
-
The "extreme" example is the Star Delegate Example and shows the technique to draw the selection rectangle when the item is selected.
-
@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): painter.save() # set background color painter.setPen(QtGui.QPen()) if option.state & QtWidgets.QStyle.State_Selected: painter.setBrush(#blue#) else: painter.setBrush(QtGui.QBrush(#white)) painter.drawRect(option.rect) # set text color value = index.data(QtCore.Qt.DisplayRole) if option.state & QtWidgets.QStyle.State_Selected: painter.setPen(QtGui.QPen(#white#) else: painter.setPen(QtGui.QPen(#black#) # Left indent painter.translate(3, 0) painter.drawText(option.rect, QtCore.Qt.AlignLeft, value) painter.restore()
(Which is a pyqt5 version of this)
In another class with the main application/window I have some table items with a custom background
-
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?
-
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.