zoukankan      html  css  js  c++  java
  • 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);

        QPropertyAnimation* rotation = 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();

        });

    }

  • 相关阅读:
    [TimLinux] CSS 纯CSS实现动画展开/收起功能
    [TimLinux] CSS pre超长自动换行
    j2ee之struts2表单细节处理
    j2ee之struts2的国际化数据方式
    j2ee之struts2拦截器()
    j2ee之struts2文件下载
    j2ee之struts2文件上传
    j2ee国际化数据方式
    j2ee监听器的实现及配置方法
    j2ee过滤器实现的主要代码
  • 原文地址:https://www.cnblogs.com/bclshuai/p/14137635.html
Copyright © 2011-2022 走看看