#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); }