Issue
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, ...?
Solution
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)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.