QT实现GIF动图显示(小白版,可直接copy使用)

需要你自己提前设置好动图的位置,本例中存放于"/Users/PLA/PLA/PLA.gif

widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMovie>
#include <QLabel>

class Widget : public QWidget {
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget(); // 添加析构函数以确保资源被正确释放

private:
    QLabel *label;
    QMovie *movie;


};

#endif // WIDGET_H

main.cpp

复制代码
#include <QApplication>
#include "widget.h"

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    Widget w;
    w.show();

    return app.exec();
}

widget.cpp

复制代码
#include "widget.h"  
#include <QVBoxLayout>  
  
Widget::Widget(QWidget *parent) : QWidget(parent), label(new QLabel(this)), movie(new QMovie("/Users/PLA/PLA/PLA.gif", QByteArray(), this)) //这里是需要你自己去调整动图的路径{  
    
    QVBoxLayout *layout = new QVBoxLayout(this);  
    layout->addWidget(label);  
  
    
    label->setMovie(movie);  
  
      
    movie->setCacheMode(QMovie::CacheAll);  
  
    
    QObject::connect(movie, &QMovie::frameChanged, [this](int frameNumber) {  
        if (movie->loopCount() == 0 && frameNumber == movie->frameCount() - 1) {  
            movie->stop();  
        }  
    });  
  
   
    movie->start();  
  
    // 调整QLabel的大小和位置(如果需要的话)  
    // 例如:label->resize(400, 300); // 注意:这可能会覆盖布局管理器设置的大小  
  
     
}  
  
Widget::~Widget() {  
    
}

效果图:

相关推荐
你好,赵志伟4 分钟前
Socket 编程 TCP
linux·服务器·tcp/ip
Liang_GaRy1 小时前
心路历程-三个了解敲开linux的大门
linux·运维·服务器
星霜笔记2 小时前
Docker 部署 MariaDB+phpMyAdmin+Nextcloud 完整教程
运维·数据库·docker·容器·mariadb
阿珊和她的猫3 小时前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
fouryears_234176 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~6 小时前
C#---StopWatch类
开发语言·c#
lifallen7 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研7 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
快乐的划水a8 小时前
组合模式及优化
c++·设计模式·组合模式
wyiyiyi8 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask