#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定时器的槽函数进行时间累计
标签:Widget,void,qTime,timer,用法,ui,timeCount,include,qTimer From: https://www.cnblogs.com/bang20221103/p/18561229