#include "basedelegate.h" #include #include #include BaseDelegate::BaseDelegate(QObject *parent, bool bottomBorderVisible, Qt::Alignment flags) : BaseItemDelegate(parent) , m_bBottomBorderVisible(bottomBorderVisible) , m_alignmentText(flags) , m_BaseHHeader(nullptr) { SetSelectedColor(QColor(67, 142, 255, 51)); } void BaseDelegate::SetBottomBorderVisible(bool value) { m_bBottomBorderVisible = value; } void BaseDelegate::SetTextAlignment(Qt::Alignment flags) { m_alignmentText = flags; } QColor BaseDelegate::GetBaseBGColor(const QStyleOptionViewItem &, const QModelIndex &) const { return QColor(70, 70, 73); } QColor BaseDelegate::GetSpecifiedBGColor(const QStyleOptionViewItem &, const QModelIndex &, SpecifiedBGRole) const { return QColor(); } QFont BaseDelegate::GetFont(const QStyleOptionViewItem &option, const QModelIndex &) const { FontEx font(qApp->font().family(), 14, false); font.setBold(option.state.testFlag(QStyle::State_None)); return font; // if(index.column() == 0) return FontEx("黑体", 14, false); // if(index.column() == 1) return FontEx("黑体", 16, false); // if(index.column() == 2) return FontEx("微软雅黑", 14, false); // if(index.column() == 3) return FontEx("微软雅黑", 16, false); // if(index.column() == 4) return FontEx("阿里巴巴普惠体", 14, false); // if(index.column() == 5) return FontEx("阿里巴巴普惠体", 16, false); //return FontEx("微软雅黑", 14, false); } QColor BaseDelegate::GetTextColor(const QStyleOptionViewItem &option, const QModelIndex &) const { QColor c = option.state.testFlag(QStyle::State_Selected)?QColor(210, 210, 210):QColor(210, 210, 210); return c; } QRect BaseDelegate::GetFontRect(const QStyleOptionViewItem &option, const QModelIndex &) const { return option.rect.adjusted(0, 0, 0, 0); } void BaseDelegate::PaintBase(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { PainterEx *painterEx = static_cast(painter); QColor baseBGColor = GetBaseBGColor(option, index); if(baseBGColor.isValid()) { painterEx->SetBrushOnly(baseBGColor); painterEx->drawRect(option.rect); } //单元格的底部线 // if(m_bBottomBorderVisible) painterEx->DrawBorder(option.rect, QColor(255, 255, 255, 38), PainterEx::RectBorderBottom); } void BaseDelegate::PaintText(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { PainterEx *painterEx = static_cast(painter); QString text = QFontMetrics(GetFont(option, index)).elidedText(index.data().toString(), Qt::ElideRight, GetFontRect(option, index).width()); painterEx->setFont(GetFont(option, index)); QRect rectText = GetFontRect(option, index); if(index.column() == 0) { rectText = QRect(QPoint(rectText.left() + 12, rectText.top()), rectText.size()); } painterEx->DrawText(rectText, text, GetTextColor(option, index), m_alignmentText); }