当前位置:网站首页>Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!
Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!
2022-08-05 04:52:00 【乔伊波伊 o(*^@^*)o】
欢迎小伙伴的点评,相互学习、互关必回、全天在线
博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神
博主主页主页
Qt制作18帧丘比特表白意中人
红酥手,黄縢酒。满城春色宫墙柳。东风恶,欢情薄。一怀愁绪,几年离索。错、错、错。
春如旧,人空瘦。泪痕红浥鲛绡透。桃花落,闲池阁。山盟虽在,锦书难托。莫、莫、莫。、
世情薄,人情恶。雨送黄昏花易落。晓风乾,泪痕残。欲笺心事,独语斜阑。难、难、难。
人成各,今非昨。病魂尝似秋千索。角声寒,夜阑珊。怕人寻问,咽泪装欢。瞒、瞒、瞒。
此时此景,请丘比特大人展现才艺了。
18帧丘比特之心,那一帧是奔向爱情的怀抱(GIF动图效果如下)

工程文件(包含图片)已经上传到github,直接用git拉取即可,命令如下
git clone https://github.com/dhn111/Qt.git
图一
图二
代码实现及详解
mainwindow.h详解(看注释“铁汁”)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void timerEvent(QTimerEvent* event); //使用timerEvent 定时器接口,对图片的播放进行延迟
void InitPixmap(); //读入照片的名称,获取路径,加载到QPixmap 数组中
void paintEvent(QPaintEvent* event); //当定时器定时通过timerEvent结束后触发接口
int eventID;
int curIndex;
private slots:
//void on_pushButton_clicked();
private:
QPixmap pixmap[18]; //声明全局变量
Ui::MainWindow *ui;
QString filename;
QPixmap map;
};
#endif // MAINWINDOW_H
动图GIf制作思路
第一步:加载图片
第二步:使用定时器延时
第三步:循环第一步和第二步做的事情
QPixmap(加载图片)和startTimer(延时)及其他接口详解
/************************************************************************/
QRect可以用一组左、顶、宽和高整数来构造,也可以用QPoint和QSize来构造。
QRect r1(640, 640, 20, 20);
QRect r2(QPoint(640, 640), QSize(20, 20));
/************************************************************************/
QPainter painter(this); //声明一个QPainter类型,使用 repaint();重新载入达到循环的目的
QPixmap map(":/res/1.png");
QRect q2(20,20,width(),height()); //设置图片在窗口的位置
painter.drawPixmap(q2,pixmap[curIndex],q2); //把变量中图片的路径用数组的方式画出来。
/************************************************************************/
int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer);
功能:启动一个定时器延时结束后触发接口timerEvent(QTimerEvent *event);
interval:定时器的触发时间(触发的间隔时间)
virtual void timerEvent(QTimerEvent *event);
功能:启动定时器后自动调用的函数(虚函数需要重写)
参数说明:event:事件(可用于判断当前发生的事件等)
/************************************************************************/
paintEvent(QPaintEvent*event)函数是QWidget类中的虚函数,
用于ui的绘制,会在多种情况下被其他函数自动调用,比如repaint()函数或者update()函数被调用时;
/************************************************************************/
mainwindow.cpp详解(看注释“铁汁”)
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPainter>
#include<QPixmap>
#include<QFileDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
resize(500,500);
eventID=startTimer(100); //延时100ms
curIndex=0;
InitPixmap(); //
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerEvent(QTimerEvent*event)
{
curIndex++; //GIF动图加载的间隔为 100ms 每隔100ms 加载一帧图片,循环18次,就实现了动图
if(curIndex>=18) //curIndex 用在 pixmap[curIndex]中,使其循环画图
{
curIndex=0;
}
repaint(); //当定时器startTimer(100) 定时结束时触发 timerEvent(QTimerEvent*event)接口
//用于重载paintEvent(QPaintEvent *event);
}
void MainWindow::InitPixmap()
{
for(int i=0;i<18;i++)
{
filename=QString(":/res/%1.png").arg(i+1);
//1_%1:切换到下一张
QPixmap map(filename); //循环获取18图片路径 ,并拷贝到数组中
pixmap[i]=map;
}
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this); //声明一个QPainter类型,使用 repaint();重新载入达到循环的目的
//QRect q(30,30,500,500);
QRect q2(20,20,width(),height()); //设置图片在窗口的位置
painter.drawPixmap(q2,pixmap[curIndex],q2); //把变量中图片的路径用数组的方式画出来。
}
#if 0
void MainWindow::on_pushButton_clicked() //保存图片到标准路径
{
QString filename = QFileDialog::getSaveFileName(this,
tr("Save Image"),
"",
tr("*.bmp;; *.png;; *.jpg;; *.tif;; *.GIF")); //选择路
pixmap->save(filename);
}
#endif
GIF动图心得
这个Demo制作动图后,无法保存到本地,不过我们可以使用专业的GIF录制工具,录制动图形成GIF保存即可,这里给大家推荐一款(GIF录制工具,链接放下面了)
screentogif官方网站

边栏推荐
- Please write the SparkSQL statement
- Talk about 20 common problems in data governance
- write the story about us
- App rapid development and construction experience: the importance of small programs + custom plug-ins
- Mini Program_Dynamic setting of tabBar theme skin
- Visibility of multi-column attribute column elements: display, visibility, opacity, vertical alignment: vertical-align, z-index The larger it is, the more it will be displayed on the upper layer
- Flutter学习5-集成-打包-发布
- 【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
- [Nine Lectures on Backpacks - 01 Backpack Problems]
- Redis - 13、开发规范
猜你喜欢
随机推荐
小程序_动态设置tabBar主题皮肤
bytebuffer 内部结构
About the installation of sklearn library
数字孪生技术在电力系统中的应用现状
In the WebView page of the UI automation test App, the processing method when the search bar has no search button
虚证、实证如何鉴别?
[8.3] Code Source - [meow ~ meow ~ meow~] [tree] [and]
bytebuffer use demo
Homework 8.4 Interprocess Communication Pipes and Signals
【cesium】3D Tileset 模型加载并与模型树关联
【转】什么是etcd
Visibility of multi-column attribute column elements: display, visibility, opacity, vertical alignment: vertical-align, z-index The larger it is, the more it will be displayed on the upper layer
Qixi Festival earn badges
The first performance test practice, there are "100 million" a little nervous
how to measure distance from point to face in creo
WPF中DataContext作用
Analyses the mainstream across technology solutions
【cesium】元素高亮显示
Mini Program_Dynamic setting of tabBar theme skin
Paparazzi: Surface Editing by way of Multi-View Image Processing









![[CISCN2019 华东南赛区]Web11](/img/15/843334fec0a5cc8cfaba92aab938db.png)