2

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 rows turned gray.

Original code from @eyllanesc:

def data(self, item, role):
    if role == Qt.BackgroundRole:
        if QSqlQueryModel.data(self, self.index(item.row(), 3), Qt.DisplayRole):
            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)

If I change it to

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

then all the rows turn yellow.

What gives? Will anyone help me understand?

N.B. I'm aware that a non-empty python string will be equivalent to True

N.B. I can replicate the desired behaviour by adding another column to the SQL query (using CASE WHEN etc.) and then using setColumnHidden(col, True) to hide the test column.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alan
  • 2,914
  • 2
  • 14
  • 26

1 Answers1

2

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Hi @eyllanesc, I know it is an old subject, but just a quick question.I see you are creating a custom class, is this a requirement? That is every time we use roles do we need to subclass or can we just create a method inside or current class?Hope my question makes sense... – Heliomaster Nov 02 '18 at 09:47
  • @Heliomaster I do not understand you, explain yourself better. – eyllanesc Nov 02 '18 at 13:04
  • Hi @eyllanesc, thanks for coming back to me. My question is lets say I have my main class like so: `class MainWindow(QMainWindow, Lmt.Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.LmtDb = LmtDataBase()` etc... could i add the data method inside this one or do I have to create a custom class and the work with roles inside this one. Hope this makes more sense... – Heliomaster Nov 03 '18 at 15:18
  • @Heliomaster okay, I understand what you point out, I gave the solution in a context but I see that it is inappropriate for your case, when you have time I will show you another option more simple and adaptable for your case with the help of a delegate. – eyllanesc Nov 03 '18 at 17:06
  • Thanks @eyllanesc. Very much willing to see how you do it. On a side note great job with your posts in general ,i’ve used a few of your examples on other subjects and they are always clear, concise and well documented. That’s what stack overflow is all about.... – Heliomaster Nov 04 '18 at 18:00
  • how to color only if clicked on the row cell – Madan Dec 09 '22 at 03:49