QT图片旋转动画

自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:

https://www.cnblogs.com/bclshuai/p/11380657.html

1.1  QT图片旋转动画

1.1.1         应用场景说明

删除图片,图片旋转滚动到垃圾箱或者删除按钮时,需要有个滚动旋转的动画效果。需要用到QGraphicsView,QGraphicsScene,QGraphicsWidget,QLabel类,QGraphicsView类相当于黑色的框,和电视的外框类似,QGraphicsScene相当于动画播放区域,下图中的黑色框内部的白色区域,在这个白色区域内播放动画。QGraphicsWidget相当于是动画区域内的一个包装类,将QLabel写上文字或者通过setpixmap接口设置图片后,添加到一个QGraphicsWidget,将QGraphicsWidget添加到QGraphicsScene,QGraphicsScene放到QGraphicsView,就可以实现旋转图片或者文字的动画效果。

 

 

 

 

1.1.2         实现方法

(1)   定义旋转角度属性

头文件定义

#ifndef ROTATIONVIEW_H

#define ROTATIONVIEW_H

 

 

#include <QGraphicsScene>

#include <QGraphicsView>

#include <QGraphicsWidget>

#include <QTextEdit>

#include <QPushButton>

#include <QGraphicsProxyWidget>

#include <QGraphicsLinearLayout>

 

class RotationView : public QGraphicsView

{

    Q_OBJECT

        Q_PROPERTY(int angle READ turnangle WRITE setturnangle)//自定义角度属性

public:

    RotationView(QWidget *parent);

    ~RotationView();

    int turnangle() { return angle; };//get方法

    void setturnangle(int angle);//set方法,动画会通过这个函数输入插值,使图片转动。

    void startMove();

private:

    QGraphicsView* view = NULL;

    int angle=0;

    QGraphicsWidget *pushButton = NULL;

};

 

#endif // ROTATIONVIEW_H

 

(2)   源文件实现

 

#include "RotationView.h"

#include<windows.h>

#include <QLabel>

#include <QPropertyAnimation>

#include <QRect>

RotationView::RotationView(QWidget *parent)

    : QGraphicsView(parent)

{

    //setWindowModality(Qt::NonModal);

    setWindowFlags(Qt::FramelessWindowHint);

    QGraphicsScene* scene = new QGraphicsScene(this);

    // 创建部件,并关联它们的信号和槽

    QPushButton *button = new QPushButton("clear");

    QLabel* plabel = new QLabel("turn me");

//  button->resize(100, 100);

    button->setGeometry(0, 0, 100, 100);

    // 将部件添加到场景中

    pushButton = scene->addWidget(plabel);

    scene->addItem(pushButton);

     view = new QGraphicsView(scene,this);

    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

     pushButton->setParent(view);

    pushButton->setPos(100, 100);

   

    view->resize(200, 200);

    //view->setStyleSheet("border:0px;");

    view->show();

   

    //view->setStyleSheet("border:0px;");

    //setturnangle(90);

}

 

RotationView::~RotationView()

{

 

}

 

void RotationView::setturnangle(int angle)

{

    QRectF r = pushButton->boundingRect();

    //for (int i = 1; i <= 100; i++)

    //{

    pushButton->setTransform(QTransform()

        .translate(r.width() / 2, r.height() / 2)

        .rotate(angle - 360 * 1, Qt::ZAxis)

        .translate(-r.width() / 2, -r.height() / 2));

    view->update();

    //}

}

 

void RotationView::startMove()

{

    QPropertyAnimation * linelength = new QPropertyAnimation(pushButton, "geometry");

    linelength->setDuration(3000);

 

    linelength->setStartValue(QRect(5,5,50,50));

    linelength->setEndValue(QRect(100, 100, 50, 50));

    linelength->setEasingCurve(QEasingCurve::Linear);

    linelength->start(QAbstractAnimation::DeleteWhenStopped);

}

(3)   定义对象设置属性动画

 

#include "qtrotation.h"

#include"RotationView.h"

#include <QPropertyAnimation>

QtRotation::QtRotation(QWidget *parent)

    : QMainWindow(parent)

{

    ui.setupUi(this);

    RotationView* roview = new RotationView(this);

    roview->setGeometry(50, 50, 200, 200);

    QPropertyAnimationrotation = newQPropertyAnimation(roview"angle"this);

    rotation->setDuration(300);

    rotation->setStartValue(0);

    rotation->setEndValue(720);

    rotation->setLoopCount(20);

    rotation->setEasingCurve(QEasingCurve::Linear);

   

    connect(ui.pushRotation, &QPushButton::clicked, this, [=]() {

        roview->startMove();

        rotation->start();

    });

}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bclshuai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值