当前位置:首页 > 通信资讯 > 正文

qt动态显示图片(qt加载动态图片)

本文实例为大家分享了Qt通过图片组绘制动态图片的具体代码,供大家参考,具体内容如下

任务实现:

通过定时器的使用来依次调用资源文件中的静态图片文件,从而达到是图片中内容动起来的效果;

效果实现:

qt动态显示图片(qt加载动态图片)

实现过程:

1.通过paintEvent()函数进行每一张图片的导入平铺绘制;
2.通过timerEvent()函数对每一张图片按照设定的时间进行重复的调用,从而达到动图的效果;
3.通过自定义InitPixmap()函数来对每一张图片进行初始化,将其导入到Pixmap[ 64 ]组中;

整体代码:

dialog.h

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #ifndef DIALOG_H #define DIALOG_H #include <QDialog> QT_BEGIN_NAMESPACE namespace Ui { class Dialog; } QT_END_NAMESPACE class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = nullptr); ~Dialog(); void paintEvent(QPaintEvent *event); void timerEvent(QTimerEvent *event); int curIndex; void InitPixmap(); private: QPixmap pixmap[64]; Ui::Dialog *ui; }; #endif // DIALOG_H

dialog.cpp

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 #include "dialog.h" #include "ui_dialog.h" #include <QPainter> #include <QPixmap> Dialog::Dialog(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog) { ui->setupUi(this); resize(160,182); startTimer(100); curIndex = 0; InitPixmap(); } Dialog::~Dialog() { delete ui; } void Dialog::paintEvent(QPaintEvent *event) { QPainter painter(this); QRect q(0,0,80,91); QRect q2(0,0,2*80,2*91); painter.drawPixmap(q2,pixmap[curIndex],q); } void Dialog::timerEvent(QTimerEvent *event) { curIndex++; if(curIndex>=64) { curIndex=0; } repaint(); } void Dialog::InitPixmap() { for(int i=0;i<64;i++) { QString filename = QString(":/Res/Resourse/1_%1.png").arg(i+1,2,10,QLatin1Char('0')); QPixmap map(filename); pixmap[i]=map; } }

调用过程

1.通过InitPixmap()函数将六十四张图片保存在Pixmap数组中;
2.通过paintEvent()函数依次调用图片;
3.通过timerEvent()函数来设定调用的循环;
4在主函数中通过定时器设定调用间隔为100ms;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_45752304/article/details/106410301

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。