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

效果图:

相关推荐
lhldsg7 小时前
全民健身解决方案软件开发实战:从架构设计到部署指南
java·前端·数据库·小程序
我是大猴子7 小时前
线上耗时问题排查
数据库
en.en..7 小时前
C语言核心解析:#define与typedef本质区别
开发语言·c++·算法
2601_966949657 小时前
批量获取多只股票五档盘口的极简方案:QuantDash Python SDK 实战指南
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
码匠许师傅8 小时前
【设计模式精讲】26.策略模式(Strategy)
c++·设计模式·策略模式·uml
小码过河.8 小时前
oceanbase中zone和observer、分区表之间关系
前端·数据库·oceanbase
ONLYOFFICE8 小时前
ONLYOFFICE成为Linux操作系统 – Winux的预装办公套件
linux·运维·服务器
其实防守也摸鱼8 小时前
OpenClaw Windows 踩坑实战指南
运维·服务器·数据库·windows·github·copilot
SelectDB8 小时前
Apache Doris+ Paimon 2.0:构建 Agentic AI 数据闭环
大数据·数据库·数据分析
新时代牛马8 小时前
嵌入式 Linux WiFi 框架完整篇:从cfg80211、mac80211 到wpa_supplicant
linux·运维·服务器