QT設(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組件布局
運行效果
源碼
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); ? ? //把定時器信號與槽函數(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++的cout.tellp()和cout.seekp()語法介紹
無論是使用 cout 輸出普通數(shù)據(jù),用 cout.put() 輸出指定字符,還是用 cout.write() 輸出指定字符串,數(shù)據(jù)都會先放到輸出流緩沖區(qū),待緩沖區(qū)刷新,數(shù)據(jù)才會輸出到指定位置,本文給大家介紹一下C++的cout.tellp()和cout.seekp()語法,需要的朋友可以參考下2023-09-09iostream與iostream.h的區(qū)別詳細(xì)解析
以下是對C++中iostream與iostream.h的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-09-09C語言數(shù)據(jù)結(jié)構(gòu)中串的模式匹配
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)中串的模式匹配的相關(guān)資料,需要的朋友可以參考下2017-05-05C語言中字符串與各數(shù)值類型之間的轉(zhuǎn)換方法
這篇文章主要介紹了C語言中字符串與各數(shù)值類型之間的轉(zhuǎn)換方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Qt實現(xiàn)導(dǎo)出QTableWidget/QTableView數(shù)據(jù)
這篇文章主要介紹了在Qt中實現(xiàn)將QTableWidget或者QTableView中的數(shù)據(jù)直接導(dǎo)出的示例代碼,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-01-01