hello, I try to change color of several QTreeWidgetItem without change text format by defaut.
my code :
Qt Code:
  1. class test(QStyledItemDelegate):
  2. def __init__(self, parent=None, *args):
  3. QItemDelegate.__init__(self, parent, *args)
  4.  
  5. def lin(self,col1,col2):
  6. o = QLinearGradient(QPointF(100, 100), QPointF(200, 200))
  7. o.setColorAt(0,col1);
  8. o.setColorAt(1, col2);
  9. return QBrush(o)
  10.  
  11. def paint(self, painter, option, index):
  12.  
  13. painter.save()
  14.  
  15. # set background color
  16. if index.sibling(index.row(),0).data(Qt.DisplayRole).toString() == "Formule":
  17. if option.state & QStyle.State_Selected:
  18. painter.setBrush( self.lin(QColor(0,0,200,100),QColor("#567dbc")) ) # Formule selected background color
  19. else:
  20. painter.setBrush(QBrush(QColor(0,100,100,150))) # Formule Normal background color
  21. painter.setPen(QPen(QBrush(QColor("#d9d9d9")),2.0)) # Formule Border color
  22. painter.drawRect(option.rect)
  23. else:
  24. if option.state & QStyle.State_Selected:
  25. painter.setBrush( self.lin(QColor(0,100,200,100),QColor("#567dbc")) ) #selected background color
  26. else:
  27. painter.setBrush(QBrush(Qt.white)) #Normal background color
  28. painter.setPen(QPen(QBrush(QColor("#d9d9d9")),2.0)) # Border color
  29. painter.drawRect(option.rect)
  30. # set text color
  31. painter.setPen(QPen(Qt.black))
  32. value = index.data(Qt.DisplayRole)
  33. if value.isValid():
  34. text = value.toString()
  35. painter.drawText(option.rect,Qt.AlignCenter, text)
  36.  
  37. painter.restore()
To copy to clipboard, switch view to plain text mode 

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.