unidbgrid 根据条件设置单元格字体颜色及其它格式

procedure TD11F.dbgD12DrawColumnCell(Sender: TObject; ACol, ARow: Integer;
    Column: TUniDBGridColumn; Attribs: TUniCellAttribs);
begin
  if Column.FieldName='DX2_13' then//变颜色的列
  begin
    if Column.Field.DataSet.FieldByName('DX2_13').AsFloat>Column.Field.DataSet.FieldByName('DX2_21').AsFloat then//条件
    begin
      Attribs.Font.Color:=clBlue;
      Attribs.Font.Size:=12;
      Attribs.Font.Style:=[fsBold]
    end    
  end;
end;

您可以通过实现QAbstractTableModel的data()函数来设置单元格字体颜色。在该函数中,您可以检查其他单元格的值,并根据需要设置当前单元格字体颜色。以下是一个示例实现: ```python from PyQt5.QtGui import QColor, QFont from PyQt5.QtCore import Qt, QAbstractTableModel class MyTableModel(QAbstractTableModel): def __init__(self, data, headers): QAbstractTableModel.__init__(self) self._data = data self._headers = headers def rowCount(self, parent): return len(self._data) def columnCount(self, parent): return len(self._data[0]) def data(self, index, role): if role == Qt.DisplayRole: return self._data[index.row()][index.column()] elif role == Qt.FontRole: # Check other cells and set font accordingly font = QFont() # Example: if value in column 0 is greater than 5, set font to bold if index.column() != 0: if self._data[index.row()][0] > 5: font.setBold(True) return font elif role == Qt.TextAlignmentRole: return Qt.AlignCenter elif role == Qt.BackgroundRole: return QColor(Qt.white) elif role == Qt.ForegroundRole: # Check other cells and set color accordingly color = QColor(Qt.black) # Example: if value in column 1 is less than 0, set color to red if index.column() != 1: if self._data[index.row()][1] < 0: color = QColor(Qt.red) return color else: return None def headerData(self, section, orientation, role): if role == Qt.DisplayRole: if orientation == Qt.Horizontal: return self._headers[section] else: return "Row %d" % section else: return None ``` 在上述示例中,我们实现了data()函数来设置单元格的字体和颜色。我们检查其他单元格的值,并根据需要设置当前单元格的字体和颜色。在本例中,我们检查第一列的值是否大于5,如果是,则将字体设置为粗体。我们还检查第二列的值是否小于0,如果是,则将字体颜色设置为红色。您可以根据需要修改此示例以满足您的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值