当前位置: 代码网 > it编程>编程语言>C/C++ > Qt QWidget实现图片旋转动画

Qt QWidget实现图片旋转动画

2024年12月29日 C/C++ 我要评论
一、效果展示二、源码分享本例程通过qgraphicsview实现svg格式图片旋转。.hpp#ifndef mainwindow_h#define mainwindow_h#include <q

一、效果展示

二、源码分享

本例程通过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旋转的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com