Qt通過(guò)圖片組繪制動(dòng)態(tài)圖片
本文實(shí)例為大家分享了Qt通過(guò)圖片組繪制動(dòng)態(tài)圖片的具體代碼,供大家參考,具體內(nèi)容如下
任務(wù)實(shí)現(xiàn):
通過(guò)定時(shí)器的使用來(lái)依次調(diào)用資源文件中的靜態(tài)圖片文件,從而達(dá)到是圖片中內(nèi)容動(dòng)起來(lái)的效果;
效果實(shí)現(xiàn):

實(shí)現(xiàn)過(guò)程:
1.通過(guò)paintEvent()函數(shù)進(jìn)行每一張圖片的導(dǎo)入平鋪繪制;
2.通過(guò)timerEvent()函數(shù)對(duì)每一張圖片按照設(shè)定的時(shí)間進(jìn)行重復(fù)的調(diào)用,從而達(dá)到動(dòng)圖的效果;
3.通過(guò)自定義InitPixmap()函數(shù)來(lái)對(duì)每一張圖片進(jìn)行初始化,將其導(dǎo)入到Pixmap[ 64 ]組中;
整體代碼:
dialog.h
#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
#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;
}
}
調(diào)用過(guò)程
1.通過(guò)InitPixmap()函數(shù)將六十四張圖片保存在Pixmap數(shù)組中;
2.通過(guò)paintEvent()函數(shù)依次調(diào)用圖片;
3.通過(guò)timerEvent()函數(shù)來(lái)設(shè)定調(diào)用的循環(huán);
4在主函數(shù)中通過(guò)定時(shí)器設(shè)定調(diào)用間隔為100ms;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決C++ fopen按行讀取文件及所讀取的數(shù)據(jù)問(wèn)題
今天小編就為大家分享一篇解決C++ fopen按行讀取文件及所讀取的數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
C++使用TinyXML2實(shí)現(xiàn)解析和生成XML數(shù)據(jù)
TinyXML2是一個(gè)輕量級(jí)的、開(kāi)源的C++庫(kù),專(zhuān)門(mén)用于解析和生成XML文檔,本文主要為大家介紹了如何使用TinyXML2實(shí)現(xiàn)解析和生成XML數(shù)據(jù),需要的可以參考下2024-04-04
C/C++題解LeetCode1295統(tǒng)計(jì)位數(shù)為偶數(shù)的數(shù)字
這篇文章主要為大家介紹了C/C++題解LeetCode1295統(tǒng)計(jì)位數(shù)為偶數(shù)的數(shù)字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

