Qt 显示gif
Qt 显示 gif
2023-09-11 14:22:56 时间
开发环境 Qt5.5.1、Qt Creator 3.5.1
Qt 与 Android 一样,如果把gif当做普通图片加载的话有且仅会显示图片的第一帧。
因此我实现了一个自定义控件,既可以加载动态的gif,也可以加载普通的图片
(1)mygifwidget.h
#ifndef MYGIFWIDGET_H
#define MYGIFWIDGET_H
#include <QLabel>
#include <QMovie>
#include <QPalette>
#include <QBrush>
#include <QResizeEvent>
#include <QPaintEvent>
#include <QPainter>
class MyGifWidget : public QLabel
{
Q_OBJECT
public:
explicit MyGifWidget(QWidget *parent = 0);
~MyGifWidget();
void resizeEvent(QResizeEvent* e);
void paintEvent(QPaintEvent* e);
bool setPath(QString path);
bool setGif(QString path);
bool setImage(QString path);
void stop();
private:
QMovie* movie;
QPixmap* pixmap;
bool setImage(QPixmap* pixmap);
};
#endif // MYGIFWIDGET_H
(2)mygifwidget.cpp
#include "mygifwidget.h"
MyGifWidget::MyGifWidget(QWidget *parent) : QLabel(parent) {
this->setAutoFillBackground(true);
movie = NULL;
pixmap = NULL;
}
MyGifWidget::~MyGifWidget() {
if(movie != NULL) delete movie;
if(pixmap != NULL) delete pixmap;
}
void MyGifWidget::resizeEvent(QResizeEvent *e) {
if(isHidden()) {
return;
}
if(movie != NULL && movie->isValid()) {
movie->setScaledSize(e->size());
} else if(pixmap != NULL && !pixmap->isNull()) {
pixmap->scaled(e->size());
}
}
void MyGifWidget::paintEvent(QPaintEvent *e) {
if(!isVisible()) {
return;
}
if(pixmap != NULL && !pixmap->isNull()) {
QPainter* painter = new QPainter(this);
painter->drawPixmap(e->rect(), *pixmap);
delete painter;
} else {
QLabel::paintEvent(e);
}
}
bool MyGifWidget::setPath(QString path) {
if(path.isEmpty()) {
return false;
}
if(path.toLower().endsWith(".gif")) {
return setGif(path);
} else {
return setImage(path);
}
}
bool MyGifWidget::setGif(QString path) {
stop();
movie = new QMovie(path);
bool result = movie->isValid();
if(result) {
movie->setScaledSize(this->size());
this->setMovie(movie);
movie->start();
}
return result;
}
bool MyGifWidget::setImage(QString path) {
stop();
pixmap = new QPixmap(path);
return setImage(pixmap);
}
bool MyGifWidget::setImage(QPixmap* pixmap) {
if(!pixmap->isNull()) {
QPalette* palette = new QPalette();
pixmap->scaled(this->size());
palette->setBrush(QPalette::Window, QBrush(*pixmap));
setPalette(*palette);
delete palette;
return true;
}
return false;
}
void MyGifWidget::stop() {
if(movie != NULL) {
movie->stop();
delete movie;
movie = NULL;
}
if(pixmap != NULL){
delete pixmap;
pixmap = NULL;
}
}
相关文章
- qt创建多层目录_Qt多工程多目录的编译案例
- Qt 一张图片显示在其他组件之上
- Qt Creator开发的程序提升到管理员权限运行
- qt中的进程优先级及应用内存大小设置方法,VS和Qt工程中申请超过2G的内存的方法
- Qt 之单例模式
- 【QT】Qt 给已经开发好的程序快速封装成动态库
- Qt编写可视化大屏电子看板系统16-标准柱状图
- Qt音视频开发44-实时人脸框
- Qt Model/View理解(二)---构造model(细心研读,发现超简单,Model就是做三件事:返回行数量、列数量、data如何显示。然后把model与view联系起来即可,两个例子都是如此)good
- QT in VS 多语言实现(中英文切换,每个步骤都有截图,只有UTF8才能让Qt语言家正确读取。先qApp->removeTranslator,然后installTranslator,每个类都要写上槽函数RetranslateUI)
- 嵌入式Qt-4.8.6显示中文并且改变字体大小和应用自己制作的字体库
- Qt之QSS(样式表语法)
- VS2010使用静态编译的qt库(Qt 5)
- Qt Creator Shortcuts 快捷键大全
- 【pyqt5学习】——bug修复,利用qt资源库qrc文件进行背景图像设置,不显示图像
- qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""
- QT QCharts QScatterSeries 空心点阵图,鼠标移动到上面显示数值,鼠标移开数值消失
- QT源码之Qt信号槽机制与事件机制的联系
- 详解 QT 源码之 Qt 事件机制原理