#include "attributeTableDelegate.h" #include AttributeTableDelegate::AttributeTableDelegate(QTableView* view, QObject *parent) : QStyledItemDelegate(parent) , m_tableView(view) { } AttributeTableDelegate::~AttributeTableDelegate() {} void AttributeTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { //先执行默认绘制(包括背景、文本等基础元素) QStyledItemDelegate::paint(painter, option, index); //处理加粗字体逻辑(在基础绘制之上叠加) if(/*index.column() == 0 || */index.data(Qt::UserRole + 100).toBool()) { QStyleOptionViewItem opt = option; initStyleOption(&opt, index); opt.font.setBold(true); //opt.palette.setColor(QPalette::Text, Qt::red); QStyledItemDelegate::paint(painter, opt, index); } //最后绘制删除线(确保位于最顶层) if(m_tableView && m_tableView->model()) { QModelIndex numberIndex = m_tableView->model()->index(index.row(), 0); QString text = numberIndex.data(Qt::DisplayRole).toString(); if(text.contains("-")) { QRect rect = m_tableView->visualRect(index); painter->save(); painter->setPen(Qt::red); painter->drawLine(rect.left(), rect.y() + rect.height()*0.5, rect.right(), rect.y() + rect.height()*0.5); //painter属于view,所以要以view的坐标系为标准 painter->restore(); } } }