#include "textColorPreserveDelegate.h" TextColorPreserveDelegate::TextColorPreserveDelegate(QObject *parent) : QStyledItemDelegate{parent} {} TextColorPreserveDelegate::~TextColorPreserveDelegate() {} void TextColorPreserveDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QColor textColor = index.data(Qt::ForegroundRole).value(); QStyleOptionViewItem opt = option; initStyleOption(&opt, index); opt.palette.setColor(QPalette::Text, textColor); opt.palette.setColor(QPalette::HighlightedText, textColor); // 关键:覆盖高亮文本色 QStyledItemDelegate::paint(painter, opt, index); }