Skip to content Skip to sidebar Skip to footer

PyQt Tableview Background Color Based On Text Value Rather Than True Or False

Follow up to my general question, where @eyllanesc has kindly answered my question. Out of curiosity, I tried changing to code to check against a string rather than 1 and all the r

Solution 1:

You should check the condition inside if role == Qt.BackgroundRole

def data(self, item, role):
    if role == Qt.BackgroundRole:
        if QSqlQueryModel.data(self, self.index(item.row(), 2), Qt.DisplayRole) == "Young":
            return QBrush(Qt.yellow)
    if role == Qt.DisplayRole:
        if item.column() == 3:
            return True if QSqlQueryModel.data(self, item, Qt.DisplayRole) == 1 else False
    return QSqlQueryModel.data(self, item, role)

enter image description here


Post a Comment for "PyQt Tableview Background Color Based On Text Value Rather Than True Or False"