Item background color from selection with custom paint() on ItemDelegate issue
-
Hello,
I noticed recently that the items on my QTreeView with custom QStyledItemDelegate paint do not get the proper background item color on select. They do on Hover/View item colors. But I need to call the QStyledItemDelegate::paint before drawing my custom data for the select color to display correctly, but it feels odd that select doesn't draw properly while the hover does. Its especially not great because if there is some data on that item, it gets painted twice, once by the QSyledItemDelegate::paint() and the second time by my own custom paint code. Is there a way to fix this? Or first draw a quad in my custom paint code with the correct color?
-
@esmolikowski said in Item background color from selection with custom paint() on ItemDelegate issue:
by the QSyledItemDelegate::paint() and the second time by my own custom paint code.
How should this happen? Please show your painting within your ItemDelegate::paint() function.
-
My paint code looks like this:
def paint(self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionViewItem, index: Union[QtCore.QModelIndex, QtCore.QPersistentModelIndex]) -> None:
painter.save()model = self.parentObject.model() sourceModel = model.sourceModel() sourceIndex = model.mapToSource(index) item = sourceModel.itemFromIndex(sourceIndex) label = self.getLabel(item) assert label opt = QtWidgets.QStyleOptionButton() opt.initFrom(label) opt.rect = option.rect.adjusted(self.padding, self.padding, -self.padding, -self.padding) opt.icon = label.icon() opt.text = label.text() opt.iconSize = label.iconSize() option.widget.style().drawControl(QtWidgets.QStyle.ControlElement.CE_PushButton, opt, painter, label) painter.restore()
Shouldn't the background color for the item already be painted with the correct color? The alternateColor and hover color for the item are properly painted because I get into this function. Only the select color is not painted properly.