PowerModeler/source/attributeTableDelegate.cpp

44 lines
1.5 KiB
C++
Raw Normal View History

#include "attributeTableDelegate.h"
#include <QPainter>
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
{
2025-03-27 21:01:25 +08:00
//先执行默认绘制(包括背景、文本等基础元素)
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);
2025-03-27 21:01:25 +08:00
//opt.palette.setColor(QPalette::Text, Qt::red);
QStyledItemDelegate::paint(painter, opt, index);
}
2025-03-27 21:01:25 +08:00
//最后绘制删除线(确保位于最顶层)
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();
}
}
}