25 lines
967 B
C++
25 lines
967 B
C++
#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);
|
||
}
|