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

效果图:

相关推荐
caimouse4 小时前
reactos编码规范
c语言·开发语言
Cosolar4 小时前
LlamaIndex索引类型全解析:原理与实战指南
运维·服务器
睡不醒男孩0308236 小时前
第二篇:深入探索开源数据库高可用:构建基于CLup的PostgreSQL生产级高可用与读写分离架构
数据库·postgresql·开源·clup
方便面不加香菜7 小时前
Linux--基础IO(一)
linux·运维·服务器
xieliyu.8 小时前
Java算法精讲:双指针(三)
java·开发语言·算法
Micro麦可乐9 小时前
Spring Boot 实战:从零设计一个短链系统(含完整代码与数据库设计)
数据库·spring boot·后端·哈希算法·雪花算法·短链系统
码农阿豪9 小时前
从零到一:Spring Boot快速接入金仓数据库实战
数据库·spring boot·后端
鼎讯信通9 小时前
风电光缆运维提质增效:G-4000A 光缆故障追踪仪破解风场巡检难题
运维·网络·数据库
CryptoPP9 小时前
快速对接东京证券交易所API数据:实战指南与代码示例
开发语言·人工智能·windows·python·信息可视化·区块链
ZC跨境爬虫9 小时前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试
开发语言·前端·javascript·学习·ecmascript