当前位置:网站首页>Qt入门(四)——连续播放图片(两种定时器的运用)
Qt入门(四)——连续播放图片(两种定时器的运用)
2022-08-08 23:05:00 【光追雨】
文章目录
一、QObject
1.1 ui设计
1.1.1 图像成果
1.1.2 类名定义
1.2 代码展示
1.2.1 widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPixmap>
#define TIMEOUT 1000
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
void timerEvent(QTimerEvent *e);
~Widget();
private slots:
void on_StartButton_clicked();
void on_StopButton_clicked();
private:
Ui::Widget *ui;
int timerId;
int PicId;
};
#endif // WIDGET_H
1.2.2 main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
1.2.3 widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
PicId = 2;
QPixmap pix("C:/Users/DELL/Desktop/pic_while/img/1.jpg");
ui->Showlabel->setPixmap(pix);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() != timerId)
{
return;
}
QString Path("C:/Users/DELL/Desktop/pic_while/img/");
Path += QString::number(PicId);
Path += ".jpg";
QPixmap pix(Path);
ui->Showlabel->setPixmap(pix);
PicId++;
if(PicId == 7)
{
PicId = 1;
}
}
void Widget::on_StartButton_clicked()
{
timerId = this->startTimer(TIMEOUT);
}
void Widget::on_StopButton_clicked()
{
this->killTimer(timerId);
}
1.3 成果展示
二、QTimer
2.1 ui设计
2.1.1 图像成果
2.1.2 类名定义
2.2 代码展示
2.2.1 widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
#define TIMEOUT 1000
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_StartButton_clicked();
void TimeoutSlots();
void on_pushButton_3_clicked();
void on_OnceButton_clicked();
private:
Ui::Widget *ui;
QTimer *timer;
int PicId;
};
#endif // WIDGET_H
2.2.2 main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
2.2.3 widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
timer = new QTimer;
PicId = 2;
ui->setupUi(this);
ui->mainlabel->setPixmap(QPixmap::fromImage(QImage("C:/Users/DELL/Desktop/QTimer/img/1.jpg")));
connect(timer,&QTimer::timeout,this,&Widget::TimeoutSlots);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_StartButton_clicked()
{
timer->start(TIMEOUT);
}
void Widget::TimeoutSlots()
{
QString Path("C:/Users/DELL/Desktop/QTimer/img/");
Path += QString::number(PicId);
Path += ".jpg";
ui->mainlabel->setPixmap(QPixmap::fromImage(QImage(Path)));
PicId++;
if(PicId == 7)
{
PicId = 1;
}
}
void Widget::on_pushButton_3_clicked()
{
timer->stop();
}
void Widget::on_OnceButton_clicked()
{
QTimer::singleShot(20,this,SLOT(TimeoutSlots()));
}
2.3 成果展示
边栏推荐
- 微信小程序 wx:for 循环输出 例子
- Firewall first contact
- wps a3格式怎么转换成a4?wps a3格式转换成a4的方法
- Ant Forest Offline crawlers automatically collect energy, raise chickens, and other operations
- 即时聊天系统中群组和好友关系Mysql数据表设计
- 如何实现call、apply、bind
- Application Layer Protocol - RADIUS
- wps表格下拉选项怎么添加?wps表格下拉选项的添加方法
- 有了国产 DevOps 工具 ,还怕数字化转型成本高?
- Pyhton面对对象
猜你喜欢
随机推荐
ALIPAY WEB 登陆rsa加密 分析记录
动手写prometheus的exporter-01-Gauge(仪表盘)
虚拟路由冗余协议VRRP——双机热备份基础
WeChat small program "decompiled" combat "behind to unpack the eggs
MES对接Simba实现展讯平台 IMEI 写号与耦合测试
The concept of GIL and pools
postman request+加密解密
Kubernetes 实现 CI/CD 发布流程
ABP中的数据过滤器
Virtual router redundancy protocol VRRP - double-machine hot backup
php7.4安装ssh2扩展和使用ssh链接sftp上传下载文件
Unity 创建重复使用的子节点,避免生成多个子节点
php判断页面访问是移动端还是pc端
The second side of Tencent technical support internship - Tencent's father's luck is so sudden (offer received)
JS中的预编译(AO、GO详解)
Application Layer Protocol - RADIUS
加载 已训练模型 张量的 几种方法
JSDay2- 长度最小的子数组
Xcode creates a Dylib plugin deb project
三国战绩 风云再起 网络版 物品序号 和 基址列表