【问题标题】:PyQt Tableview background color based on numeric values?基于数值的PyQt Tableview背景颜色?
【发布时间】:2018-12-22 10:46:30
【问题描述】:

有一个很好的例子说明如何实现"PyQt Tableview background color based on text value" 因为我不允许发表评论,所以我必须提出一个新问题。

如何将“id”列与整数进行比较?例如如果(id>103)和(id

代码行取自上面的链接:

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

似乎对文本工作正常,但是

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

将作为文本而不是整数进行比较,并且

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

给出一个 AttributeError: 'QVariant' 对象没有属性 'toInt'

如何将单元格值比较为整数、浮点数、布尔值...?

【问题讨论】:

  • 你用的是什么版本的 PyQt5?
  • 我在 Win7 上使用 PyQt5 5.10 版

标签: python python-3.x pyqt pyqt5 qtableview


【解决方案1】:

您必须使用QVariantvalue() 方法,如果需要强制转换。

val = QSqlQueryModel.data(self, self.index(item.row(), 0), Qt.DisplayRole)
if int(val.value()) > 102:
    return QBrush(Qt.yellow)

【讨论】:

  • 非常感谢@eyllanesc,我被 QVariant 方法 toInt()、toFloat()、toBool()、...误导了。
猜你喜欢
  • 2017-10-21
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
  • 2014-05-24
  • 2016-10-25
  • 2014-08-17
相关资源
最近更新 更多