Issue
I'm using PyQt6 and have a custom QTableView and a custom QTableModel. The model's data method looks like this:
def data(self, index: QModelIndex, role: Qt.ItemDataRole = Qt.ItemDataRole.DisplayRole):
if role == Qt.ItemDataRole.DecorationRole:
if not index.column():
# print(index.row())
artwork_pixmap = self._tracks[index.row()].artwork_pixmap
artwork_pixmap = artwork_pixmap if artwork_pixmap else QPixmap(f"icons/album.png")
icon = QIcon(artwork_pixmap)
return icon
_tracks is a list containing custom objects which have the pixmap I want to display, and if they don't then I just display default pixmap from image album.png. That all works fine, the QIcons get displayed in the first column. But once I select the row it also makes the QIcon blueish, which isn't what I want. And it makes it blueish regardless of the selection color set in the stylesheet of my view. Even if I set it's focus policy to NoFocus, it doesn't select anything on click but it still makes the QIcon in the row I clicked in turn blueish. Is there any way to prevent this?
Here is the example, the first row is the one I clicked in, the background color hasn't change because of NoFocus, but the QIcon became blueish in comparison to it's normal color.
Solution
QIcon automatically creates alternate versions of the given pixmap for Disabled
and Selected
icon modes, the first is a grayed out version of the pixmap, the second overlays a layer based on the palette Highlight
role.
To override those icons, just explicitly set the pixmap for the desired state with addPixmap()
:
artwork_pixmap = self._tracks[index.row()].artwork_pixmap
if not artwork_pixmap:
artwork_pixmap = QPixmap("icons/album.png")
icon = QIcon(artwork_pixmap)
icon.addPixmap(artwork_pixmap, QtGui.QIcon.Selected)
return icon
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.