QT設(shè)計秒表功能(跑步計時器)
本文實(shí)例為大家分享了QT設(shè)計秒表功能的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)計目標(biāo)
1. 定時器開始
2.復(fù)位從0開始計時
3.記錄--把記錄的時間添加到QTextBrowser, append(時間)
4. QTime t(0,0,0) t = t.addMsec( number ) t.toString (“hh:mm:ss:zzz”)
定時器(QTimer)的使用
定時器---定時發(fā)送信號timeout
QTimer 定時器類
1.創(chuàng)建定時器類對象
QTimer mtimer;
2.把定時器信號與槽函數(shù)關(guān)聯(lián)
connect(&mtimer, &QTimer::timeout, this, &TimerWin::on_outBt_clicked);
3.啟動定時器
mtimer.start(1000);
4.停止定時器
mtimer.stop();
QT Creator組件布局

運(yùn)行效果

源碼
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_Hstopwatchwin.cpp
#include "stopwatchwin.h"
#include "ui_stopwatchwin.h"
#include <QDebug>
StopwatchWin::StopwatchWin(QWidget *parent) :
? ? QMainWindow(parent),
? ? ui(new Ui::StopwatchWin)
{
? ? ui->setupUi(this);
? ? //把定時器信號與槽函數(shù)關(guān)聯(lián)
? ? 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
#include "stopwatchwin.h"
#include <QApplication>
?
int main(int argc, char *argv[])
{
? ? QApplication a(argc, argv);
? ? StopwatchWin w;
? ? w.show();
?
? ? return a.exec();
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++代碼實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++代碼實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
c++之std::get_time和std::put_time
std::get_time和std::put_time是C++中用于日期和時間的格式化和解析的函數(shù),它們都包含在<iomanip>頭文件中,std::get_time用于從輸入流中解析日期時間字符串,而std::put_time則用于將std::tm結(jié)構(gòu)格式化為字符串2024-10-10
C++ 多線程編程建議之 C++ 對多線程/并發(fā)的支持(下)
這篇文章主要介紹的是 C++ 多線程編程建議之 C++ 對多線程/并發(fā)的支持的相關(guān)資料,承接前文 現(xiàn)代 C++ 對多線程/并發(fā)的支持,接下來我們看看回發(fā)生什么吧2021-10-10
遞歸法求最大公約數(shù)和最小公倍數(shù)的實(shí)現(xiàn)代碼
今天整理了一下用遞歸法求最大公約數(shù)(gcd)和最小公倍數(shù)(lcm)。主要的工作是求最大公約數(shù)。數(shù)學(xué)上可以用輾轉(zhuǎn)法求最大公約數(shù)2013-05-05
C語言實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02
C/C++函數(shù)參數(shù)聲明解析int?fun()?與?int?fun(void)?的區(qū)別講解
C++中int fun()和int fun(void)的區(qū)別在于函數(shù)參數(shù)的聲明方式,前者默認(rèn)允許任意參數(shù),而后者表示沒有參數(shù),通過清晰的實(shí)例源代碼,詳細(xì)解釋了它們在函數(shù)聲明和調(diào)用中的不同之處,這篇文章介紹了C/C++函數(shù)參數(shù)聲明int?fun()與int?fun(void)的差異,需要的朋友可以參考下2024-01-01

