本文實例為大家分享了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