PyQt Tableview 行背景颜色基于单元格值
PyQt Tableview row background colour based on cell value
我正在使用 Python3+ 和 Qt5(尽管很高兴有 Py2.7 和 Qt4 答案!)。
完全被关于样式、委托、模型和其他一切的大量文档搞糊涂了。
我发现设置交替行的背景很简单,但我想为其中一列与特定值匹配的行设置背景(即 Archive == True
)。
备用行:
self.plainModel = QSqlQueryModel()
self.create_model()
self.linksTable.setModel(self.plainModel)
self.linksTable.setAlternatingRowColors(True)
self.linksTable.setStyleSheet("alternate-background-color: Lightgrey;background-color: white;")
self.linksTable.resizeColumnsToContents()
我看过一个展示如何做的例子 through the model 但这个具体的例子似乎只是简单地复制交替行的结果,在盯着代码看了几天后我无法弄清楚如何将其转化为检查存档列。
摘自example:
elif role == Qt.BackgroundRole:
if index.row() % 2 == 0:
return QBrush(Qt.yellow)
elif role != Qt.DisplayRole:
return QVariant()
我找到了另一个 example using delegates,但目前无法理解它。
特别是我仍然无法理解您如何选择更改哪些行,也无法理解如何应用简单的背景颜色 "option"! (阅读有关 QStyleOptionViewItem 的文档让我陷入困境!)。
你能帮忙吗?
我们必须获取归档列中的数据(在我的示例中是第三个)并验证它是否满足条件(在本例中为真),如果是这样我们return QBrush
具有所需颜色的对象。
def data(self, item, role):
if role == Qt.BackgroundRole:
if QSqlQueryModel.data(self, self.index(item.row(), 3), Qt.DisplayRole):
return QBrush(Qt.yellow)
return QSqlQueryModel.data(self, item, role)
另外,在我的例子中使用数据库 SQLITE,其中没有布尔数据,但使用限制为值 0 或 1 的数据类型 int 进行模拟,因此使用以下指令 returns True 或 False 根据分别为 1 或 0。
def data(self, item, role):
[...]
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)
总之使用下面的代码,另外完整的代码是here:
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)
截图:
我正在使用 Python3+ 和 Qt5(尽管很高兴有 Py2.7 和 Qt4 答案!)。 完全被关于样式、委托、模型和其他一切的大量文档搞糊涂了。
我发现设置交替行的背景很简单,但我想为其中一列与特定值匹配的行设置背景(即 Archive == True
)。
备用行:
self.plainModel = QSqlQueryModel()
self.create_model()
self.linksTable.setModel(self.plainModel)
self.linksTable.setAlternatingRowColors(True)
self.linksTable.setStyleSheet("alternate-background-color: Lightgrey;background-color: white;")
self.linksTable.resizeColumnsToContents()
我看过一个展示如何做的例子 through the model 但这个具体的例子似乎只是简单地复制交替行的结果,在盯着代码看了几天后我无法弄清楚如何将其转化为检查存档列。
摘自example:
elif role == Qt.BackgroundRole:
if index.row() % 2 == 0:
return QBrush(Qt.yellow)
elif role != Qt.DisplayRole:
return QVariant()
我找到了另一个 example using delegates,但目前无法理解它。
特别是我仍然无法理解您如何选择更改哪些行,也无法理解如何应用简单的背景颜色 "option"! (阅读有关 QStyleOptionViewItem 的文档让我陷入困境!)。
你能帮忙吗?
我们必须获取归档列中的数据(在我的示例中是第三个)并验证它是否满足条件(在本例中为真),如果是这样我们return QBrush
具有所需颜色的对象。
def data(self, item, role):
if role == Qt.BackgroundRole:
if QSqlQueryModel.data(self, self.index(item.row(), 3), Qt.DisplayRole):
return QBrush(Qt.yellow)
return QSqlQueryModel.data(self, item, role)
另外,在我的例子中使用数据库 SQLITE,其中没有布尔数据,但使用限制为值 0 或 1 的数据类型 int 进行模拟,因此使用以下指令 returns True 或 False 根据分别为 1 或 0。
def data(self, item, role):
[...]
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)
总之使用下面的代码,另外完整的代码是here:
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)
截图: