五十三、自定义实现QGraphicsItem

一、效果展示

在这里插入图片描述

二、代码实现

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选中样式

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值