当前位置:网站首页>Introduction to Qt (4) - Continuous playback of pictures (the use of two timers)
Introduction to Qt (4) - Continuous playback of pictures (the use of two timers)
2022-08-08 23:07:00 【light chases rain】
文章目录
一、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 成果展示
边栏推荐
猜你喜欢
随机推荐
wps a列不见了怎么办?wps a列不见了的解决方法
第二课:概率论
PHP regular to img SRC to add the domain name
全国各省市区行政代码 城市代码.txt
用工具实现 Mock API 的整个流程
最详树莓派4B装机流程及ifconfig不到wlan0的解决办法
Kubernetes 实现 CI/CD 发布流程
主从延迟原因及解决方案
Analysis of WLAN - Wireless Local Area Network
LeetCode:最长有效括号
WeChat small program "decompiled" combat "behind to unpack the eggs
待完善:tf.name_scope() 和 tf.variable_scope()的区别
数组去重的几种方法
Qt入门(四)——连续播放图片(两种定时器的运用)
Xcode 创建一个Dylib 插件deb项目
php7.4安装ssh2扩展和使用ssh链接sftp上传下载文件
DHCP的防御机制——DHCP Snooping(DHCP监听)
flutter 书写json解析类
The second side of Tencent technical support internship - Tencent's father's luck is so sudden (offer received)
PMP考点有哪些啊?