Qt QWidget实现图片旋转动画
一、效果展示

二、源码分享
本例程通过QGraphicsView实现svg格式图片旋转。

.hpjavascriptp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicssvgItem>
#include <QGraphicsScene>
#include <QTimer>
#include <QPropertyAnimation>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QGraphicsSvgItem *graphItem;
QGraphicsScene *graphScene;
};
#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->graphScene = new QGraphicsScene();
this->ui->graphicsView->setScene(this->graphScene);
this->graphItem = new QGraphicsSvgItem( ":/image/running.svg" );
this->graphItem->setScale(0.5);
QRectF boundingRect = this->graphItem->boundingRect();
this->graphItem->setTransformOriginPoint(boundingRect.width() / 2, boundingRect.height() / 2);
graphScene->addItem( this->graphItem );
this->graphItem->setRotation(45);
// 编程创建一个QPropertyAnimation对象来控制旋转属性
QPropertyAnimation* rotationAnimation = new QPropertyAnimation(this->graphItem, "rotation");
// 设置动画的起始值和结束值
rotationAnimation->setStartValue(0);
rotationAnimationwww.devze.com->setEndValue(360);
// 设置动画持续时间(以毫秒为单位)
rotationAnimation->setDurandroidation(3000);
// 设置动画循环次数(-1表示无限循环)
rotationAnimation->setLoopCount(-1);
// 启动动画
rotationAnimation->start();
this->ui->gr编程aphicsView->installEventFilter(this);
this->ui->graphicsView->centerOn(this->graphItem);
}
MainWindow::~MainWindow()
{
delete ui;
}
以上就是Qt QWidget实现图片旋转动画的详细内容,更多关于Qt QWidget旋转的资料请关注编程客栈(www.devze.com)其它相关文章!
加载中,请稍侯......
精彩评论