I have a Qt application that uses a QTableView and a QListView. In both witdgets I display some icons.
The problem is that when the user selects one of the entries Qt alpha blends the selection over the icons making the icons appear with less contrast and slightly different colors.
I want Qt to draw the selection first and then draw the icons on top so they look the same no matter if they are selected or not. How can I do that?
Update:
This is what my problem looks like:
It is easier to spot with a white rectangular icon:
When you check the color value with a graphics editor you see that the color value changes. I would like Qt to paint the icon as is without this overlay.
The QListView here is styled with a color gradient background. But I have the same problem with a QTableView which is unstyled.
All icons are SVG by the way.
The drawing of ItemView elements is handled by a delegate. You can set a custom delegate for a column, a row, or for individual items. The default implementation just delegates drawing to the widget's style:
You can find the actual code that does the drawing here. It seems that all styles shipped with Qt5 ultimately delegate the drawing of ItemView elements to that code. What's interesting is that code in question actually draws the icon on top of the selection, so you must be using some custom style. The call that draws the selection is:
That call goes before all the other drawing calls.
In your custom delegate you should be able to ask the style to draw everything except the icon, by modifying
QStyleOptionViewItem opt
(see the first code snippet). Then you can draw you icon manually, the same wayQCommonStyle
does it.