QGraphicsPixmapItem QPropertyAnimation QTransform 自定义图片控件旋转、缩放 图形视图框架( 三)

一、效果图
在这里插入图片描述
二、代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef FLASHITEM_H
#define FLASHITEM_H

#include <QWidget>
namespace Ui {
class flashItem;
}
class item;
class flashItem : public QWidget
{
    Q_OBJECT

public:
    explicit flashItem(QWidget *parent = nullptr);
    ~flashItem();

private:
    Ui::flashItem *ui;
};


#include <QGraphicsItem>
#include <QGraphicsPixmapItem>
#include <QPainter>
#include <QTimer>
#include <QTransform>
class ItemClass : public QObject , public QGraphicsPixmapItem
{
    Q_OBJECT
    Q_PROPERTY(int RotateAngle READ RotateAngle WRITE setRotateAngle)
public:
    explicit ItemClass(QObject *parent = nullptr, const int& type = int());
    QRectF boundingRect() const override;

    int RotateAngle();
    void setRotateAngle(int angle);
private:
    int m_angle;
    int m_type;

};

#endif // FLASHITEM_H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "flashitem.h"
#include "ui_flashitem.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPropertyAnimation>
flashItem::flashItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::flashItem)
{
    ui->setupUi(this);
    QGraphicsScene *scene=new QGraphicsScene;
    scene->setSceneRect(-200,-200,400,400);

    ItemClass *flashItem = new ItemClass(this,0);
    flashItem->setPixmap(QPixmap(":/image/1.jpg").scaled(200,200));
    flashItem->setFlag(QGraphicsItem::ItemIsMovable);
    flashItem->setPos(0,0);

    QPropertyAnimation *animation = new QPropertyAnimation(flashItem,"RotateAngle");
    animation->setStartValue(0);
    animation->setEndValue(360);
    animation->setDuration(2000);
    animation->setEasingCurve(QEasingCurve::Linear);
    animation->setLoopCount(-1);
    animation->start();

    ItemClass *flashItem1 = new ItemClass(this,1);
    flashItem1->setPixmap(QPixmap(":/image/1.jpg").scaled(200,200));
    flashItem1->setFlag(QGraphicsItem::ItemIsMovable);
    flashItem1->setPos(0,0);

    QPropertyAnimation *animation1 = new QPropertyAnimation(flashItem1,"RotateAngle");
    animation1->setStartValue(0);
    animation1->setEndValue(360);
    animation1->setDuration(2000);
    animation1->setEasingCurve(QEasingCurve::Linear);
    animation1->setLoopCount(-1);
    animation1->start();

    ItemClass *flashItem2 = new ItemClass(this,2);
    flashItem2->setPixmap(QPixmap(":/image/1.jpg").scaled(200,200));
    flashItem2->setFlag(QGraphicsItem::ItemIsMovable);
    flashItem2->setPos(0,0);

    QPropertyAnimation *animation2 = new QPropertyAnimation(flashItem2,"RotateAngle");
    animation2->setStartValue(0);
    animation2->setEndValue(360);
    animation2->setDuration(100);
    animation2->setEasingCurve(QEasingCurve::Linear);
    animation2->setLoopCount(-1);
    animation2->start();

    ItemClass *flashItem3 = new ItemClass(this,3);
    flashItem3->setPixmap(QPixmap(":/image/1.jpg").scaled(200,200));
    flashItem3->setFlag(QGraphicsItem::ItemIsMovable);
    flashItem3->setPos(0,0);

    QPropertyAnimation *animation3 = new QPropertyAnimation(flashItem3,"RotateAngle");
    animation3->setStartValue(1);
    animation3->setEndValue(3);
    animation3->setDuration(2100);
    animation3->setEasingCurve(QEasingCurve::Linear);
    animation3->setLoopCount(-1);
    animation3->start();

    scene->addItem(flashItem);
    scene->addItem(flashItem1);
    scene->addItem(flashItem2);
    scene->addItem(flashItem3);


    QGraphicsView *view = new QGraphicsView(this);
    view->setRenderHint(QPainter::Antialiasing);
    view->setScene(scene);
    view->setMinimumSize(1400,1400);
    view->show();

    show();
}

flashItem::~flashItem()
{
    delete ui;
}


ItemClass::ItemClass(QObject *parent, const int &type)
{
    m_type = type;
}

QRectF ItemClass::boundingRect() const
{
    return QRectF(0,0,200,200);
}

int ItemClass::RotateAngle()
{
    return m_angle;
}

void ItemClass::setRotateAngle(int angle)
{
    m_angle = angle;
    if(m_type == 0)
        setTransform(QTransform().rotate(angle, Qt::XAxis));
    else if(m_type == 1)
        setTransform(QTransform().rotate(angle, Qt::YAxis));
    else if(m_type == 2){
        setTransform(QTransform().
                     translate(this->boundingRect().width()/2,this->boundingRect().height()/2).
                     rotate(angle, Qt::ZAxis).
                     translate(0-this->boundingRect().width()/2,0-this->boundingRect().height()/2));

    }else if(m_type == 3)
        setTransform(QTransform().scale(angle,angle));
}