一、效果展示

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

.hpp
#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);
    rotationanimation->setendvalue(360);
    // 设置动画持续时间(以毫秒为单位)
    rotationanimation->setduration(3000);
    // 设置动画循环次数(-1表示无限循环)
    rotationanimation->setloopcount(-1);
    // 启动动画
    rotationanimation->start();
    this->ui->graphicsview->installeventfilter(this);
    this->ui->graphicsview->centeron(this->graphitem);
}
mainwindow::~mainwindow()
{
    delete ui;
}
以上就是qt qwidget实现图片旋转动画的详细内容,更多关于qt qwidget旋转的资料请关注代码网其它相关文章!
            
                                            
                                            
                                            
                                            
                                            
发表评论