根据单独单元格中的值为QTableView单元格着色

时间:2020-05-07 18:16:22

标签: pyqt5

我想根据另一个单元格中的值格式化一个单元格。我正在使用由QAbstractModel填充到熊猫数据框的QTableView。

基本上如下所示,如果第1列的单元格中的值为'WE',我想在第5列的相应单元格中格式化BackgroundRole。

非常感谢您的协助!

class StateModel(QtCore.QAbstractTableModel):
    def __init__(self, imported_dataframe=pd.DataFrame()):
        QtCore.QAbstractTableModel.__init__(self)
        self._dataframe = imported_dataframe

    def setDataFrame(self, imported_dataframe):
        self.beginResetModel()
        self._dataframe = imported_dataframe
        self.endResetModel()

    def rowCount(self, parent=None):
        return self._dataframe.shape[0]

    def columnCount(self, parent=None):
        return self._dataframe.shape[1]

    def data(self, index, role=QtCore.Qt.DisplayRole):
        current_column = index.column()
        current_row = index.row()

        if index.isValid():
            if role == QtCore.Qt.ForegroundRole:

                if current_column == 1:
                    it = self._dataframe.iloc[index.row(), current_column]
                    if it == 'WE':
                        return QtGui.QBrush(QtCore.Qt.white)

            if role == QtCore.Qt.BackgroundColorRole:
                if current_column == 1:
                    it = self._dataframe.iloc[index.row(), current_column]
                    if it == 'WE':
                        return QtGui.QBrush(QtCore.Qt.blue)

            if role == QtCore.Qt.FontRole:
                table_font = QtGui.QFont('open sans', 12)
                return table_font

            if role == QtCore.Qt.DisplayRole:
                return str(self._dataframe.iloc[index.row(), index.column()])

    def headerData(self, col, orientation, role):
        header_font = QtGui.QFont('open sans', 14, QtGui.QFont.Bold)
        black_color = QtGui.QColor(QtCore.Qt.blue)
        if orientation == QtCore.Qt.Horizontal:
            if role == QtCore.Qt.DisplayRole:
                return str(self._dataframe.columns[col])
            if role == QtCore.Qt.FontRole:
                return header_font

1 个答案:

答案 0 :(得分:0)

答案就是@Heike所暗示的。

{Schema: 
{"type":"record","name":"TheCustomerDecisionEvent",
"namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro","fields":
[{"name":"eventCreatedTimestamp","type":["null","long"]},
{"name":"iouDecision","type":["null",{"type":"enum",
"name":"TheType",
"namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro",
"symbols":["HRMR","FIELD","PLANE"]}]},
{"name":"iouDecisionReason","type":["null",{"type":"record","name":"iouDecisionReason",
"namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro",
"fields":[{"name":"value","type":["null","string"]}]}]}]}
,
 contents: { eventCreatedTimestamp: 1588694347577, 
 iouDecision: Schema: {"type":"enum","name":"TheType",
 "namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro",
 "symbols":["HRMR","FIELD","PLANE"]}, value: PLANE,
 iouDecisionReason: Schema: {"type":"record","name":"iouDecisionReason",
"namespace":"com.farmxxx.tst.property.service.options.iou.messaging.avro",
"fields":[{"name":"value","type":["null","string"]}]}, contents: { value: Optimal, }, }}