PDA

View Full Version : [PYQT] QTreeWidget item delegate problem !



GMib
12th November 2010, 22:26
hello, I try to change color of several QTreeWidgetItem without change text format by defaut.
my code :

class test(QStyledItemDelegate):
def __init__(self, parent=None, *args):
QItemDelegate.__init__(self, parent, *args)

def lin(self,col1,col2):
o = QLinearGradient(QPointF(100, 100), QPointF(200, 200))
o.setColorAt(0,col1);
o.setColorAt(1, col2);
return QBrush(o)

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.setPen(QPen(QBrush(QColor("#d9d9d9")),2.0)) # Formule Border 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.setPen(QPen(QBrush(QColor("#d9d9d9")),2.0)) # Border 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.