• Qt入门(四)——连续播放图片(两种定时器的运用)


    一、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 // WIDGET_H
    
    
    • 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
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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 // WIDGET_H
    
    
    • 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();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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 成果展示

    请添加图片描述

  • 相关阅读:
    什么牌子的蓝牙耳机耐用?平价蓝牙耳机里面性价比高的推荐
    【高分快刊】Elsevier旗下,中科院2区SCI,2个月19天录用!
    力扣刷题 day41:10-11
    Appium手机Android自动化
    Neo4J超详细专题教程,快来收藏起来吧
    SSM框架速成——mybatis速成总结
    vue-主题切换
    告别人工智障:Mengzi开源项目团队获亿元人民币融资,助力NLP发展
    机器学习 笔记06:最大熵模型
    PostgreSql 认证方式-Peer 认证
  • 原文地址:https://blog.csdn.net/m0_52592798/article/details/126228117