PowerModeler/source/attributeTableDelegate.cpp

50 lines
1.6 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-04-01 18:10:48 +08:00
//根据行号设置交替色
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
if ((index.row() + 1) % 2 == 0)
{
//opt.backgroundBrush = QBrush(QColor(243, 245, 249));
painter->fillRect(opt.rect, QColor(240, 248, 255));
}
2025-03-27 21:01:25 +08:00
2025-04-01 18:10:48 +08:00
//处理加粗字体逻辑
2025-03-27 21:01:25 +08:00
if(/*index.column() == 0 || */index.data(Qt::UserRole + 100).toBool())
{
opt.font.setBold(true);
2025-03-27 21:01:25 +08:00
//opt.palette.setColor(QPalette::Text, Qt::red);
}
2025-03-27 21:01:25 +08:00
2025-04-01 18:10:48 +08:00
//先执行默认绘制(包括背景、文本等基础元素)
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();
}
}
}