PowerModeler/source/attributeTableDelegate.cpp

44 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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
{
//先执行默认绘制(包括背景、文本等基础元素)
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();
}
}
}