qTime及qTimer用法

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>
#include <QTime>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void slotTimeOut();
    void slotBtnStart();
    void slotBtnStop();
private:
    Ui::Widget *ui;
    QTimer m_timer;
    QTime m_timeCount;
};
#endif // WIDGET_H
复制代码
复制代码
#include "widget.h"
#include "./ui_widget.h"
#include<qDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(ui->btn_start,SIGNAL(clicked()),this,SLOT(slotBtnStart()));
    connect(ui->btn_stop,SIGNAL(clicked()),this,SLOT(slotBtnStop()));
    connect(&m_timer,&QTimer::timeout,this,&Widget::slotTimeOut);

    m_timeCount = QTime(0,0,0,0);   //使用前一定要初始化,不然无法得到值

}

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

void Widget::slotTimeOut()
{
    int interval =m_timer.interval();
    m_timeCount= m_timeCount.addMSecs(interval);
    ui->lineEdit->setText(m_timeCount.toString("hh:mm:ss"));
}

void Widget::slotBtnStart()
{
    m_timer.start(1000);
}

void Widget::slotBtnStop()
{
    m_timer.stop();
}
复制代码

功能实现:响应Qtimer定时器的槽函数进行时间累计

 

posted @   BangZeng  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示