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() {  
    
}

效果图:

相关推荐
Adios79441 分钟前
设置交集大小至少为2
数据结构·算法·leetcode
海阔天空任鸟飞~1 小时前
Linux 权限 777
linux·运维·服务器
流星白龙1 小时前
【Redis】2.Redis重大版本
数据库·redis·junit
流星白龙1 小时前
【Redis】7.Hash表
数据库·redis·哈希算法
流星白龙2 小时前
【Redis】4.基本全局命令
数据库·redis·缓存
程序喵大人3 小时前
【C++进阶】STL容器与迭代器 - 01 STL 容器先解决元素放在哪里
开发语言·c++·stl
王八八。3 小时前
Navicat 17破解版下载安装教程 附安装激活步骤(2026 最新版)
数据库·navicat
cui_ruicheng3 小时前
Python从入门到实战(十六):多进程编程
开发语言·python
Jelena157795857924 小时前
电商运营分析数据比价接口实战:多平台价格监控与智能决策系统
java·大数据·数据库
Kina_C4 小时前
Linux iptables 防火墙原理与实操——从四表五链到 NAT 配置
linux·运维·服务器·iptables