QT系列教程(16) 定时器事件

定时器

Qt中提供了两种方式实现定时器,第一种是通过startTimer的方式启动定时器,该函数返回定时器的id,然后我们需要为实现定时器的类重写timerEvent。我们先介绍这一种,创建Qt Application项目,项目默认的类名为Widget,继承自QWidget。

重写timerEvent

我们在QWidget类里先声明三个定时器的id,并且声明我们要重写的timerEvent事件

cpp 复制代码
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    virtual void timerEvent(QTimerEvent *event);
private:
    Ui::Widget *ui;
    int id1;
    int id2;
    int id3;
};

接下来我们在构造函数中启动三个定时器

cpp 复制代码
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    id1 = startTimer(1000);
    id2 = startTimer(1500);
    id3 = startTimer(2000);
}

在析构函数中回收三个定时器

cpp 复制代码
Widget::~Widget()
{
    delete ui;
    killTimer(id1);
    killTimer(id2);
    killTimer(id3);
}

然后重写timerEvent,进而实现三个定时器的功能

cpp 复制代码
void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == id1){
        qDebug() << "id1 timer triggered" ;
    }

    if(event->timerId() == id2){
        qDebug() << "id2 timer triggered" ;
    }

    if(event->timerId() == id3){
        qDebug() << "id3 timer triggered" ;
    }
}

运行程序后,就可以看到三个定时器被调用,并且根据每个定时器的id输出信息。

QTimer实现定时器

我们在Widget的声明中,添加一个槽函数

cpp 复制代码
class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    virtual void timerEvent(QTimerEvent *event);
    void slot_update_led();
private:
    Ui::Widget *ui;
    int id1;
    int id2;
    int id3;
};

接下来我们在Widget的构造函数中初始化一个定时器,并且连接定时器发出的信号和槽函数,然后启动定时器

cpp 复制代码
QTimer * timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Widget::slot_update_led);
timer->start(1000);

然后我们在ui里添加一个lcdnumber的控件,用来显示当前的时间,然后在定时器的槽函数slot_update_led中设置lcdnumber显示时间信息

cpp 复制代码
void Widget::slot_update_led()
{
       QTime time = QTime::currentTime();
       QString text = time.toString("hh:mm:ss");
       //qDebug() << " current time is " << text;
       if(time.second()%2==0){
           text[5] = ' ';
           text[2] = ' ';
       }
       ui->lcdNumber->setDigitCount(text.length());
       ui->lcdNumber->display(text);
}

程序运行后,就可以根据定时器每个一秒获取时间信息显示在lcdNumber上了。

源码链接

源码链接

th());

ui->lcdNumber->display(text);

}

复制代码
程序运行后,就可以根据定时器每个一秒获取时间信息显示在lcdNumber上了。
## 源码链接
源码链接
[https://gitee.com/secondtonone1/qt-learning-notes](https://gitee.com/secondtonone1/qt-learning-notes)
相关推荐
skywalk81631 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾1 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
听情歌落俗2 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力2 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
ShineWinsu2 小时前
对于Qt:QFontDialog的解析
c++·qt·面试·gui·字体·客户端·qfontdialog
落羽的落羽4 小时前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法
小飞在努力5 小时前
【实现负载均匀在线判题④】接入 cpp-httplib 实现编译服务 HTTP 网络化
c语言·开发语言·c++·笔记·http
炸薯条!6 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
CappuccinoRose6 小时前
前端开发交互体系 - 汇总篇
开发语言·前端·javascript·css·html·交互
tryCbest7 小时前
Python 爬虫从入门到实战(二):进阶篇——反爬对抗、并发与动态页面
开发语言·爬虫·python