[PYQT] QTreeWidget item delegate problem !
hello, I try to change color of several QTreeWidgetItem without change text format by defaut.
my code :
Code:
class test(QStyledItemDelegate):
def __init__(self, parent=None, *args):
def lin(self,col1,col2):
o.setColorAt(0,col1);
o.setColorAt(1, col2);
def paint(self, painter, option, index):
painter.save()
# set background color
if index.sibling(index.row(),0).data(Qt.DisplayRole).toString() == "Formule":
if option.
state & QStyle.
State_Selected: painter.
setBrush( self.
lin(QColor(0,
0,
200,
100),
QColor("#567dbc")) ) # Formule selected background color else:
painter.
setBrush(QBrush(QColor(0,
100,
100,
150))) # Formule Normal background color painter.drawRect(option.rect)
else:
if option.
state & QStyle.
State_Selected: painter.
setBrush( self.
lin(QColor(0,
100,
200,
100),
QColor("#567dbc")) ) #selected background color else:
painter.
setBrush(QBrush(Qt.
white)) #Normal background color painter.drawRect(option.rect)
# set text color
painter.
setPen(QPen(Qt.
black)) value = index.data(Qt.DisplayRole)
if value.isValid():
text = value.toString()
painter.drawText(option.rect,Qt.AlignCenter, text)
painter.restore()
with this code, text is more small, the font size is not shrink if text is too big and i can't choose align text when a create QTreeWidgetItem :(
someone know how too change color whithout change text formating ?
Sorry for my bad english.