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();

    });

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM