1.IDE:QTCreator
2.实验:
(1)使用QOBJECT的TIMER
(2)EVENT时间
(3)多定时器定时溢出判断
(4)QLABEL填充图片
3.记录
4.代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
virtual void timerEvent(QTimerEvent *event); //重构函数
private slots:
void on_start_pb_clicked();
void on_stop_pb_clicked();
private:
Ui::Widget *ui;
int timer_id; //记录定时器ID
uint8_t album_id; //记录图片ID
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QPixmap pixmap(":album/imag/fj1.png");
ui->album->setPixmap(pixmap);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_start_pb_clicked() //开始按钮按下
{
timer_id=this->startTimer(3000); //开启3秒定时器
}
void Widget::timerEvent(QTimerEvent *event) //重构定时器函数
{
if(event->timerId()==timer_id)
{
album_id++;
if(album_id==4) //只有三张图片
album_id=1;
QString path=":album/imag/fj"+QString::number(album_id)+".png";
QPixmap pix(path);
ui->album->setPixmap(pix);
}
}
void Widget::on_stop_pb_clicked() //停止按钮按下
{
this->killTimer(timer_id);
}