Qt::QListView
#ifndef MUITEIMAGEELEGATE_H #define MUITEIMAGEELEGATE_H #include <QStyledItemDelegate> #include <QMetaType> typedef struct { QString strIds; QString strImageType; QString strPicName; QString strUpdateTime; QString strBase64; } MuItemImageData; Q_DECLARE_METATYPE(MuItemImageData) class PGMultiImageDelegate : public QStyledItemDelegate { Q_OBJECT public: PGMultiImageDelegate(QObject *parent = nullptr); // painting void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE; bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index); signals: void itemClicked(int); }; #endif // MUITEMDELEGATE_H
#include "StdAfx.h" #include <QPainter> #include <QDebug> #include "PGMultiImageDelegate.h" #include <QPainter> #define PIXMAP_SIZE 80 PGMultiImageDelegate::PGMultiImageDelegate(QObject *parent) : QStyledItemDelegate(parent) { } void PGMultiImageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.isValid()) { painter->save(); QVariant var = index.data(Qt::UserRole+1); MuItemImageData itemData = var.value<MuItemImageData>(); // item 矩形区域 QRectF rect; rect.setX(option.rect.x()); rect.setY(option.rect.y()); rect.setWidth(option.rect.width()-1); rect.setHeight(option.rect.height()-1); QPainterPath path; path.moveTo(rect.topRight()); path.lineTo(rect.topLeft()); path.quadTo(rect.topLeft(), rect.topLeft()); path.lineTo(rect.bottomLeft()); path.quadTo(rect.bottomLeft(), rect.bottomLeft()); path.lineTo(rect.bottomRight()); path.quadTo(rect.bottomRight(), rect.bottomRight()); path.lineTo(rect.topRight()); path.quadTo(rect.topRight(), rect.topRight()); // 鼠标悬停或者选中时改变背景色 if (option.state.testFlag(QStyle::State_MouseOver)) { painter->setPen(QPen(QColor("#ebeced"))); painter->setBrush(QColor("#ebeced")); painter->drawPath(path); } if (option.state.testFlag(QStyle::State_Selected)) { painter->setPen(QPen(QColor("#e3e3e5"))); painter->setBrush(QColor("#e3e3e5")); painter->drawPath(path); } // 绘制图片 QPixmap pixmap; pixmap.loadFromData(QByteArray::fromBase64(itemData.strBase64.toLocal8Bit())); pixmap.scaled(PIXMAP_SIZE, PIXMAP_SIZE, Qt::KeepAspectRatio); QRectF iconRect = QRect(rect.left(), rect.top(), PIXMAP_SIZE, PIXMAP_SIZE); painter->drawImage(iconRect, QImage(pixmap.toImage())); //设备名称 QRectF rectDevName = QRect(iconRect.right()+5, iconRect.top(), rect.width()-10-iconRect.width(), 20); painter->setPen(QPen(Qt::black)); painter->setFont(QFont("Microsoft Yahei", 8)); painter->drawText(rectDevName, itemData.strImageType); QRectF rectName = QRect(rectDevName.left(), rectDevName.bottom() + 5, rect.width() - 10 - iconRect.width(), 20); painter->setPen(QPen(Qt::gray)); painter->drawText(rectName, itemData.strPicName); QRectF rectUpdateTime = QRect(rectName.left(), rectName.bottom() + 5, rect.width() - 10 - iconRect.width(), 20); painter->setPen(QPen(Qt::gray)); painter->drawText(rectUpdateTime, itemData.strUpdateTime); painter->restore(); } } bool PGMultiImageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if (event->type() == QEvent::MouseButtonRelease) { emit itemClicked(index.row()); } return true;// QStyledItemDelegate::editorEvent(event, model, option, index); } QSize PGMultiImageDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(index) return QSize(option.rect.width(), PIXMAP_SIZE); }
void PGMediaCardDlg::_initImageData() { if (m_ImageInfoList.count() == 0) return; m_pModel->clear(); m_qImageListView->resizeMode(); for (int i = 0; i < m_ImageInfoList.count(); ++i) { QStandardItem *pItem = new QStandardItem; MuItemImageData itemData; pItem->setData(QVariant::fromValue(itemData), Qt::UserRole + 1); m_pModel->appendRow(pItem); } PGMultiImageDelegate *pItemDelegate = new PGMultiImageDelegate(this); m_qImageListView->setItemDelegate(pItemDelegate); m_qImageListView->setModel(m_pModel); connect(pItemDelegate, SIGNAL(itemClicked(int)), this, SLOT(slotImageListViewItemClicked(int))); }