一、QObject
1.1 ui设计
1.1.1 图像成果

1.1.2 类名定义

1.2 代码展示
1.2.1 widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#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
- 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
1.2.2 main.cpp
#include "widget.h"
#include
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
- 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
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
#include
#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
- 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
2.2.2 main.cpp
#include "widget.h"
#include
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()));
}
- 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
2.3 成果展示
