logplus/logPlus/backgrounddelegate.cpp

25 lines
967 B
C++
Raw Normal View History

#include "backgrounddelegate.h"
#include <QPainter>
#include <QPixmap>
BackgroundDelegate::BackgroundDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
void BackgroundDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// 首先检查这个单元格是否有背景图片数据我们可以使用一个角色来存储比如Qt::UserRole+1
QVariant bgData = index.data(Qt::UserRole+1); // 我们约定用这个角色存储图片路径
if (bgData.isValid()) {
QString imagePath = bgData.toString();
QPixmap pixmap(imagePath);
if (!pixmap.isNull()) {
// 绘制背景图片,缩放到单元格大小
painter->drawPixmap(option.rect, pixmap.scaled(option.rect.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}
}
// 然后调用基类绘制文本等
QStyledItemDelegate::paint(painter, option, index);
}