如何实现QT编程中的秒表功能,用于跑步计时器设计?

更新于
2026-10-10 14:01:34
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计593个文字,预计阅读时间需要3分钟。

如何实现QT编程中的秒表功能,用于跑步计时器设计?

原文示例:本文字例为大家分享了QT设计秒表功能的整体代码,供大家参考。具体内容如下:

设计目标

1.定时器开始

2.复位从0开始计时

3.记录--把记录的时间添加到QTextBrowser

append(时间)

改写后:本例展示了QT设计秒表功能的代码示例,包括:- 设计目标:定时器启动、复位计时、记录时间并显示在QTextBrowser中。

本文实例为大家分享了QT设计秒表功能的具体代码,供大家参考,具体内容如下

设计目标

1. 定时器开始

2.复位从0开始计时

3.记录--把记录的时间添加到QTextBrowser, append(时间)

4. QTime t(0,0,0) t = t.addMsec( number ) t.toString (“hh:mm:ss:zzz”)

定时器(QTimer)的使用

定时器---定时发送信号timeout
QTimer 定时器类

1.创建定时器类对象
QTimer mtimer;

2.把定时器信号与槽函数关联
connect(&mtimer, &QTimer::timeout, this, &TimerWin::on_outBt_clicked);

3.启动定时器
mtimer.start(1000);

4.停止定时器
mtimer.stop();

QT Creator组件布局

运行效果

源码

stopwatchwin.h

#ifndef STOPWATCHWIN_H #define STOPWATCHWIN_H   #include <QMainWindow> #include <QTime> #include <QTimer> namespace Ui { class StopwatchWin; }   class StopwatchWin : public QMainWindow {     Q_OBJECT   public:     explicit StopwatchWin(QWidget *parent = nullptr);     ~StopwatchWin();     void fun_clicked();   private slots:     void on_pushButton_clicked();       void on_startBt_clicked();       void on_stopBtn_clicked();       void on_recordBtn_clicked();       void on_resertBt_clicked();   private:     Ui::StopwatchWin *ui;     //QTime t;     QTime t = QTime(0,0,0,0);     QTimer mtimer; };   #endif // STOPWATCHWIN_H

stopwatchwin.cpp

#include "stopwatchwin.h" #include "ui_stopwatchwin.h" #include <QDebug> StopwatchWin::StopwatchWin(QWidget *parent) :     QMainWindow(parent),     ui(new Ui::StopwatchWin) {     ui->setupUi(this);     //把定时器信号与槽函数关联     connect(&mtimer, &QTimer::timeout, this, &StopwatchWin::fun_clicked); }   StopwatchWin::~StopwatchWin() {     delete ui; }   void StopwatchWin::fun_clicked() {     QString tim = t.toString("hh:mm:ss:zzz");     t = t.addMSecs(10);     ui->lcdNumber->display(tim);     qDebug()<<"1111"; } void StopwatchWin::on_startBt_clicked() {     qDebug()<<"启动定时器";     mtimer.start(10); }   void StopwatchWin::on_stopBtn_clicked() {     qDebug()<<"停止定时器";     if(mtimer.isActive())     {         mtimer.stop();     } }   void StopwatchWin::on_pushButton_clicked() {   }   void StopwatchWin::on_recordBtn_clicked() {     QString tim = t.toString("hh:mm:ss:zzz");     ui->textBrowser->append(tim); }   void StopwatchWin::on_resertBt_clicked() {    t = QTime(0,0,0,0); }

main.cpp

如何实现QT编程中的秒表功能,用于跑步计时器设计?

#include "stopwatchwin.h" #include <QApplication>   int main(int argc, char *argv[]) {     QApplication a(argc, argv);     StopwatchWin w;     w.show();       return a.exec(); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计593个文字,预计阅读时间需要3分钟。

如何实现QT编程中的秒表功能,用于跑步计时器设计?

原文示例:本文字例为大家分享了QT设计秒表功能的整体代码,供大家参考。具体内容如下:

设计目标

1.定时器开始

2.复位从0开始计时

3.记录--把记录的时间添加到QTextBrowser

append(时间)

改写后:本例展示了QT设计秒表功能的代码示例,包括:- 设计目标:定时器启动、复位计时、记录时间并显示在QTextBrowser中。

本文实例为大家分享了QT设计秒表功能的具体代码,供大家参考,具体内容如下

设计目标

1. 定时器开始

2.复位从0开始计时

3.记录--把记录的时间添加到QTextBrowser, append(时间)

4. QTime t(0,0,0) t = t.addMsec( number ) t.toString (“hh:mm:ss:zzz”)

定时器(QTimer)的使用

定时器---定时发送信号timeout
QTimer 定时器类

1.创建定时器类对象
QTimer mtimer;

2.把定时器信号与槽函数关联
connect(&mtimer, &QTimer::timeout, this, &TimerWin::on_outBt_clicked);

3.启动定时器
mtimer.start(1000);

4.停止定时器
mtimer.stop();

QT Creator组件布局

运行效果

源码

stopwatchwin.h

#ifndef STOPWATCHWIN_H #define STOPWATCHWIN_H   #include <QMainWindow> #include <QTime> #include <QTimer> namespace Ui { class StopwatchWin; }   class StopwatchWin : public QMainWindow {     Q_OBJECT   public:     explicit StopwatchWin(QWidget *parent = nullptr);     ~StopwatchWin();     void fun_clicked();   private slots:     void on_pushButton_clicked();       void on_startBt_clicked();       void on_stopBtn_clicked();       void on_recordBtn_clicked();       void on_resertBt_clicked();   private:     Ui::StopwatchWin *ui;     //QTime t;     QTime t = QTime(0,0,0,0);     QTimer mtimer; };   #endif // STOPWATCHWIN_H

stopwatchwin.cpp

#include "stopwatchwin.h" #include "ui_stopwatchwin.h" #include <QDebug> StopwatchWin::StopwatchWin(QWidget *parent) :     QMainWindow(parent),     ui(new Ui::StopwatchWin) {     ui->setupUi(this);     //把定时器信号与槽函数关联     connect(&mtimer, &QTimer::timeout, this, &StopwatchWin::fun_clicked); }   StopwatchWin::~StopwatchWin() {     delete ui; }   void StopwatchWin::fun_clicked() {     QString tim = t.toString("hh:mm:ss:zzz");     t = t.addMSecs(10);     ui->lcdNumber->display(tim);     qDebug()<<"1111"; } void StopwatchWin::on_startBt_clicked() {     qDebug()<<"启动定时器";     mtimer.start(10); }   void StopwatchWin::on_stopBtn_clicked() {     qDebug()<<"停止定时器";     if(mtimer.isActive())     {         mtimer.stop();     } }   void StopwatchWin::on_pushButton_clicked() {   }   void StopwatchWin::on_recordBtn_clicked() {     QString tim = t.toString("hh:mm:ss:zzz");     ui->textBrowser->append(tim); }   void StopwatchWin::on_resertBt_clicked() {    t = QTime(0,0,0,0); }

main.cpp

如何实现QT编程中的秒表功能,用于跑步计时器设计?

#include "stopwatchwin.h" #include <QApplication>   int main(int argc, char *argv[]) {     QApplication a(argc, argv);     StopwatchWin w;     w.show();       return a.exec(); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。