#include "ordertypedelegate.h" // ==================== OrderTypeDelegate ==================== OrderTypeDelegate::OrderTypeDelegate(QObject *parent) : QStyledItemDelegate(parent) { } void OrderTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (!index.isValid()) return; // 保存原始渲染状态 painter->save(); // 获取单元格文本 QString type = index.data(Qt::DisplayRole).toString(); // 设置背景色 if (type == "买入") { painter->fillRect(option.rect, m_buyColor); } else if (type == "卖出") { painter->fillRect(option.rect, m_sellColor); } // 恢复原始渲染状态 painter->restore(); // 添加悬停效果 if (option.state & QStyle::State_MouseOver) { QColor hoverColor = (type == "买入") ? m_buyColor.lighter(120) : m_sellColor.lighter(120); painter->fillRect(option.rect, hoverColor); } QStyledItemDelegate::paint(painter, option, index); } void OrderTypeDelegate::setBuyColor(const QColor &color) { m_buyColor = color; } void OrderTypeDelegate::setSellColor(const QColor &color) { m_sellColor = color; } NumberFormatDelegate::NumberFormatDelegate(QObject *parent) : QStyledItemDelegate(parent) { } // 暂时不动,后面只在显示端进行格式化,方便统一处理数据 QString NumberFormatDelegate::displayText(const QVariant &value, const QLocale &locale) const { bool ok; double num = value.toDouble(&ok); if (ok) { // 格式化为带千位分隔符的数字 return locale.toString(num, 'f', 0); } return QStyledItemDelegate::displayText(value, locale); } HighlightDelegate::HighlightDelegate(QObject *parent) : QStyledItemDelegate(parent) { } void HighlightDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem opt = option; initStyleOption(&opt, index); // 检查是否需要高亮 - 通过映射到源模型获取高亮状态 QAbstractItemModel *model = const_cast(index.model()); QSortFilterProxyModel *proxyModel = qobject_cast(model); bool isHighlighted = false; if (proxyModel) { QModelIndex sourceIndex = proxyModel->mapToSource(index); isHighlighted = sourceIndex.data(IsHighlightedRole).toBool(); } else { isHighlighted = index.data(IsHighlightedRole).toBool(); } if (isHighlighted) { // 设置高亮背景 painter->fillRect(opt.rect, QColor(255, 192, 203)); // 浅黄色背景 // 设置加粗字体 QFont boldFont = opt.font; boldFont.setBold(true); painter->setFont(boldFont); // 绘制文本 painter->setPen(opt.palette.color(QPalette::Text)); painter->drawText(opt.rect, opt.displayAlignment, opt.text); } else { // 正常绘制 QStyledItemDelegate::paint(painter, opt, index); } }