一、效果展示
二、代码实现
CustomGraphicsItem.h
#ifndef UICANVASBASEITEM_H
#define UICANVASBASEITEM_H
#include <QObject>
#include <QGraphicsPixmapItem>
class CustomGraphicsItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
CustomGraphicsItem(QGraphicsItem *parent = 0);
CustomGraphicsItem(QString &imageFile, QString &text, int imageSize = 0);
~CustomGraphicsItem();
void setMyPixmap(QString &imageFile, int imageSize);
QString getText() const;
void setText(const QString &value);
QSize getImageSize();
protected:
QRectF boundingRect() const Q_DECL_OVERRIDE;
QPainterPath shape() const Q_DECL_OVERRIDE;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE;
private:
QString text; //图标下的文字
QSize imageRealSize; //图像大小
};
#endif // UICANVASBASEITEM_H
CustomGraphicsItem.cpp
#include "customgraphicsitem.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
CustomGraphicsItem::CustomGraphicsItem(QGraphicsItem *parent) : QGraphicsPixmapItem(parent)
{
}
CustomGraphicsItem::CustomGraphicsItem(QString &imageFile, QString &text, int imageSize)
{
setMyPixmap(imageFile, imageSize);
setText(text);
}
CustomGraphicsItem::~CustomGraphicsItem()
{
}
void CustomGraphicsItem::setMyPixmap(QString &imageFile, int imageSize)
{
QPixmap pixmap;
pixmap.load(imageFile);
if (imageSize != 0) {
pixmap = pixmap.scaled(imageSize, imageSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
setPixmap(pixmap);
this->imageRealSize = QSize(pixmap.width(), pixmap.height());
}
QString CustomGraphicsItem::getText() const
{
return text;
}
void CustomGraphicsItem::setText(const QString &value)
{
text = value;
}
QRectF CustomGraphicsItem::boundingRect() const
{
QRect rect = this->pixmap().rect();
return QRectF(0, 0, rect.width(), rect.width() + 15);
}
QPainterPath CustomGraphicsItem::shape() const
{
QRectF rect = boundingRect();
QPainterPath path;
//矩形边角半径
path.addRoundRect(rect, 5, 5);
return path;
}
void CustomGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QPixmap pixMap = this->pixmap();
QRect rect = pixMap.rect();
painter->drawPixmap(rect, pixMap);
QPen pen(Qt::black);
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing);
QFont font("Verdana", 8, QFont::Normal);
painter->setFont(font);
painter->drawText(QRectF(0, rect.height(), rect.width(), 15), Qt::AlignCenter, text);
//自定义选中时的样式(若这里不设置,选中是无样式的)
if (option->state & QStyle::State_Selected) {
qreal itemPenWidth = painter->pen().widthF();
const qreal pad = itemPenWidth / 2;
const qreal penWidth = 0;
//边框区域颜色
QColor color = QColor(Qt::red);
//绘制实线
// painter->setPen(QPen(color, penWidth, Qt::SolidLine));
// painter->setBrush(Qt::NoBrush);
// painter->drawRect(boundingRect().adjusted(pad, pad, -pad, -pad));
//绘制虚线
painter->setPen(QPen(color, 0, Qt::DashLine));
painter->setBrush(Qt::NoBrush);
painter->drawRect(boundingRect().adjusted(pad, pad, -pad, -pad));
}
}
QSize CustomGraphicsItem::getImageSize()
{
return imageRealSize;
}
参见qt学习笔记(五) QGraphicsPixmapItem与QGraphicsScene的编程实例 图标拖动渐变效果
参见自定义QGraphicsItem选中样式