1

There is a nice example of how to achieve "PyQt Tableview background color based on text value" since I am not allowed to comment I have to open a new question.

How can I compare column "id" as integer numbers? e.g. if (id>103) and (id<106)?

Code line taken from link above:

if QSqlQueryModel.data(self, self.index(item.row(), 2), Qt.DisplayRole) == "Young":

seems to work fine for text, but

if QSqlQueryModel.data(self, self.index(item.row(), 0), Qt.DisplayRole) > "103":

would compare as text not as integer, and

if QSqlQueryModel.data(self, self.index(item.row(), 0), Qt.DisplayRole).toInt() > 103:

gives an AttributeError: 'QVariant' object has no attribute 'toInt'

How to compare cell values as integer, float, boolean, ...?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
theozh
  • 22,244
  • 5
  • 28
  • 72

1 Answers1

2

You have to use the value() method of QVariant and if casting is necessary.

val = QSqlQueryModel.data(self, self.index(item.row(), 0), Qt.DisplayRole)
if int(val.value()) > 102:
    return QBrush(Qt.yellow)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • thanks a lot @eyllanesc, I was mislead by the QVariant methods toInt(), toFloat(), toBool(), ... – theozh Jul 15 '18 at 13:57