使用Qt编写定时器,实现图片的循环显示

本文详细介绍了在Qt中使用QObject和QTimer两种方法实现定时器,通过定时器控制图片的切换,并展示了如何操作开始、暂停和单次显示图片的功能。
摘要由CSDN通过智能技术生成

项目架构

1、首先创建好两个Qt项目 ,一个是QObjectTime, 一个是QTime

2、制作ui界面

 

 

一、使用QObject实现

定义定时器时间 :

#define TIMEOUT 1*1000

widget.h中:

private slots:
    void on_startButton_clicked();

    void on_stopButton_clicked();

private:
    Ui::Widget *ui;
    int myTimeId ;   // 定时器编号
    int picID;       // 记录是第几张图片

widget.cpp 中:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    picID = 2 ;    // 图片id从2开始

    // // 图片的路径
    QPixmap pix("C:\\test\\1.jpg");    
    ui->label->setPixmap(pix) ;
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_startButton_clicked()
{
    // 开始定时器,返回定时器编号
    myTimeId = this->startTimer(TIMEOUT) ;
}

void Widget::timerEvent(QTimerEvent *event)
{
    if( event->timerId() != myTimeId )
    {
        return ;
    }

    // 图片的路径
    QString path("C:\\test\\\");

    path += QString::number(picID);
    path += ".jpg" ;

    QPixmap pix(path);
    ui->label->setPixmap(pix) ;


    // 图片Id 加1
    picID ++ ;

    // 图片可以循环
    if( picID == 5)
    {
        picID = 1 ;
    }

}


// 暂停
void Widget::on_stopButton_clicked()
{
    this->killTimer(myTimeId) ;
}

二、使用QTimer实现

定义定时器时间 :

#include <QTimer>   // 添加头文件

#define TIMEOUT 1*1000  // 定义定时器时间

widget.h中:

private slots:
    void on_startButton_clicked();     // 开始

    void timeOutSlot() ;               // 自定义槽函数

    void on_stopButton_clicked();      // 暂停

    void on_singleButton_clicked();    // 单次

private:
    Ui::Widget *ui;

    QTimer *timer ;     // 指针
    int picID ;         // 图片编号

widget.cpp 中:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    timer = new QTimer; // 创建定时器对象

    picID = 2 ;

    QImage img ;

    // 加载图片
    img.load("C:\\test\\1.jpg");

    // 显示图片
    ui->label->setPixmap(QPixmap::fromImage(img)) ;

    // 定时器时间到, 发出timeout信号
    connect(timer, &QTimer::timeout, this, &Widget::timeOutSlot ) ;
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_startButton_clicked()
{
    // 开始定时器
    timer->start(TIMEOUT) ;
}

void Widget::timeOutSlot()
{
    // 图片路径
    QString path("C:test\\");
    path += QString::number(picID);
    path += ".jpg" ;

    // 加载显示图片
    QImage img ;
    img.load(path);
    ui->label->setPixmap(QPixmap::fromImage(img)) ;

    // 图片Id 加1 
    picID ++ ;
    if( 5 == picID )
    {
        picID = 1 ;
    }

}


//  暂停
void Widget::on_stopButton_clicked()
{
    timer->stop() ;
}


// 单次
void Widget::on_singleButton_clicked()
{
    // 单次显示图片
    QTimer::singleShot(1000, this, SLOT(timeOutSlot())) ;

}

三、效果展示

视频没有弄好,就不放上来了,大家自行尝试。

 

未完待续


有疑问的小伙伴可以留言一起交流讨论!!!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值