logplus/logPlus/backgrounddelegate.cpp

25 lines
967 B
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 "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);
}