Qt實現(xiàn)簡易秒表設(shè)計
Qt–簡易秒表設(shè)計(QTimer,Qtime,TableWiget應(yīng)用),供大家參考,具體內(nèi)容如下
效果圖
使用QTimer和QTime兩個類
思路:
1.計時功能:?
利用QTimer中的定時器中斷信號,設(shè)置每10毫秒觸發(fā)一次timeout信號,在對應(yīng)槽函數(shù)中對LCD number中顯示的時間進行更新,即LCD屏中每十毫秒更新一次;對于時間的累加顯示(QTimer是定時器)需要Qtime,在每一次timeout信號出發(fā)時,使Qtime類的time累加上10ms。
部分關(guān)鍵代碼如下(代碼并非連續(xù),只說關(guān)鍵點,源碼在末尾):
//.h文件中添加頭文件 #include <QTimer> #include <QTime> //聲明 ? ? QTimer * m_Timer;?? ?//定時器,用來每10ms發(fā)出timeout信號 ? ? QTime m_Time;?? ?//用來計時 ? ? QTime showTime;?? ?//往lcd上顯示的時間 // .cpp文件里 構(gòu)造函數(shù)中進行初始化 ? ?? ?m_Timer = new QTimer; ? ? m_Time.setHMS(0, 0, 0, 0);?? ?// //設(shè)置timeout間隔10ms ?? ?m_Timer->start(10); //每次timeout讓m_Time加10ms ?? ?connect(m_Timer, &QTimer::timeout, this, &app::updateDisplayTime); ?? ?//曹函數(shù)如下 ?? ?void app::updateDisplayTime() ? ? { ? ? ? ? m_Time = m_Time.addMSecs(10);//計時器累加10ms ? ? ? ? QString tim = m_Time.toString("mm:ss.zzz");//時間轉(zhuǎn)換為字符串 ?? ??? ? ? ? ?? ?ui->lcdNumber_Timer->display(tim.left(tim.length() - 1));//將字符串最后一個0去掉 ? ? }
時間的顯示與更新邏輯解決后,其次是如何使用***tableWiget***控件實現(xiàn)計次功能,以下簡單介紹tableWiget使用與計次功能實現(xiàn):
//計次按鈕對應(yīng)的槽函數(shù) ?? ?int m_Row = 0;//行數(shù) ?? ?if(ui->pushButton_Count->text() == "計次") ? ? { ? ? ? ? QString tim = m_Time.toString("mm:ss.zzz");//獲取時間 ? ? ? ? ui->tableWidget->insertRow(m_Row);//插入行,每次點擊計次都需要新加入一行? ? ? ? ? //每次新增一個格子(不是一行)都需要new一個QTableWigetitem ? ? ? ? ui->tableWidget->setItem(m_Row, 0, new QTableWidgetItem(tr("#%0").arg(m_Row+1)));//0行,0列為序號 ? ? ? ? ui->tableWidget->setItem(m_Row, 1, new QTableWidgetItem("計次"));//0行,1列為動作 ? ? ? ? ui->tableWidget->setItem(m_Row, 2, new QTableWidgetItem(tim.left(tim.length() - 1)));//計次時間 ? ? ? ? m_Row++; ? ? }
2.按鍵設(shè)計
?按下開始按鈕后,開始按鈕變?yōu)橥V梗?按下停止后停止變?yōu)槔^續(xù)并且計次變?yōu)閺?fù)位,,主要邏輯類似于iphone手機里的計時器,自己點點就明白了,主要是邏輯問題,詳情見源碼:
3.源碼
(有很多地方可以單獨寫成函數(shù)的,此處就先這樣吧,主要是總結(jié)思路,勿噴)
#ifndef APP_H #define APP_H #include <QWidget> #include <QTimer> #include <QTime> QT_BEGIN_NAMESPACE namespace Ui { class app; } QT_END_NAMESPACE class app : public QWidget { ? ? Q_OBJECT public: ? ? app(QWidget *parent = nullptr); ? ? ~app(); private slots: ? ? void on_pushButton_Start_clicked(); ? ? void on_pushButton_Count_clicked(); private: ? ? Ui::app *ui; ? ? QTimer * m_Timer; ? ? QTime m_Time; ? ? QTime showTime; ? ? int m_Row; public: ? ? void updateDisplayTime(); }; #endif // APP_H
#include "app.h" #include "ui_app.h" #include <QDebug> app::app(QWidget *parent) ? ? : QWidget(parent) ? ? , ui(new Ui::app) { ? ? ui->setupUi(this); ? ? m_Timer = new QTimer; ? ? m_Time.setHMS(0, 0, 0, 0); ? ? m_Row = 0; ? ? //初始化顯示 ? ? ui->lcdNumber_Timer->display("00:00.00"); ? ? QStringList Header; ? ? Header << "序號" << "動作" << "計次"; ? ? ui->tableWidget->setColumnCount(3); ? ? ui->tableWidget->setHorizontalHeaderLabels(Header); ? ? //開始,暫停,計次事件 ? ? connect(m_Timer, &QTimer::timeout, this, &app::updateDisplayTime); } app::~app() { ? ? delete ui; } //更新l'c'd中的時間 void app::updateDisplayTime() { ? ? qDebug() << "timeout"; ? ? QString tim = m_Time.toString("mm:ss.zzz"); ? ? m_Time = m_Time.addMSecs(10); ? ? ui->lcdNumber_Timer->display(tim.left(tim.length() - 1)); } // 開始, 與 暫停, 判斷,如果按鈕名稱為開始 void app::on_pushButton_Start_clicked() { ? ? //qDebug() << "startBtn"; ? ? //啟動定時器,并設(shè)置timeout的中斷間隔為10毫秒 ? ? if(ui->pushButton_Start->text() == "開始") ? ?//按下時是開始,開始計時,并且此按鍵變?yōu)橥V? ? ? { ? ? ? ? m_Timer->start(10); ? ? ? ? m_Row = 0; ? ? ? ? ui->tableWidget->clearContents(); ? ? ? ? ui->tableWidget->setRowCount(0); ? ? ? ? ui->pushButton_Start->setText("停止"); ? ? ? ? ui->pushButton_Count->setText("計次"); ? ? } ? ? else if(ui->pushButton_Start->text() == "停止") ? //按下時是停止,停止計時,并且按鍵變?yōu)槔^續(xù) ? ? { ? ? ? ? ui->pushButton_Start->setText("繼續(xù)"); ? ? ? ? ui->pushButton_Count->setText("復(fù)位"); ? ? ? ? ui->tableWidget->insertRow(m_Row); ? ? ? ? ui->tableWidget->setItem(m_Row, 0, new QTableWidgetItem(tr("#%0").arg(m_Row+1))); ? ? ? ? ui->tableWidget->setItem(m_Row, 1, new QTableWidgetItem("停止")); ? ? ? ? //按鈕為停止時按下,停止時間 ? ? ? ? m_Timer->stop(); ? ? ? ? QString tim = m_Time.toString("mm:ss.zzz"); ? ? ? ? ui->tableWidget->setItem(m_Row, 2, new QTableWidgetItem(tim.left(tim.length() - 1))); ? ? ? ? m_Row++; ? ? } ? ? else if(ui->pushButton_Start->text() == "繼續(xù)") ? ? { ? ? ? ? ui->pushButton_Start->setText("停止"); ? ? ? ? ui->pushButton_Count->setText("計次"); ? ? ? ? m_Timer->start(10); ? ? } ? ? else ? ? { ? ? ? ? return; ? ? } } //計次與復(fù)位 void app::on_pushButton_Count_clicked() { ? ? if(ui->pushButton_Count->text() == "計次") ? ? { ? ? ? ? QString tim = m_Time.toString("mm:ss.zzz"); ? ? ? ? ui->tableWidget->insertRow(m_Row); ? ? ? ? ui->tableWidget->setItem(m_Row, 0, new QTableWidgetItem(tr("#%0").arg(m_Row+1))); ? ? ? ? ui->tableWidget->setItem(m_Row, 1, new QTableWidgetItem("計次")); ? ? ? ? ui->tableWidget->setItem(m_Row, 2, new QTableWidgetItem(tim.left(tim.length() - 1))); ? ? ? ? m_Row++; ? ? } ? ? else if(ui->pushButton_Count->text() == "復(fù)位") ? //復(fù)位時將另一個按鈕變?yōu)殚_始 ? ? { ? ? ? ? m_Time.setHMS(0,0,0,0); ? ? ? ? ui->lcdNumber_Timer->display("00:00.00"); ? ? ? ? ui->pushButton_Start->setText("開始"); ? ? } ? ? else ? ? { ? ? ? ? return; ? ? } } text() == "復(fù)位") ? //復(fù)位時將另一個按鈕變?yōu)殚_始 ? ? { ? ? ? ? m_Time.setHMS(0,0,0,0); ? ? ? ? ui->lcdNumber_Timer->display("00:00.00"); ? ? ? ? ui->pushButton_Start->setText("開始"); ? ? } ? ? else ? ? { ? ? ? ? return; ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言中求字符串長度的函數(shù)的幾種實現(xiàn)方法
這篇文章主要介紹了C語言中求字符串長度的函數(shù)的幾種實現(xiàn)方法,需要的朋友可以參考下2018-08-08基于linux下C開發(fā)中的幾點技術(shù)經(jīng)驗總結(jié)
本篇文章是對linux下C開發(fā)中的幾點技術(shù)經(jīng)驗總結(jié)進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C/C++?Qt?數(shù)據(jù)庫與TreeView組件綁定詳解
本篇文章主要介紹了QT數(shù)據(jù)庫與View組件的綁定,通過數(shù)據(jù)庫與組件關(guān)聯(lián)可實現(xiàn)動態(tài)展示數(shù)據(jù)庫中的表記錄。感興趣的小伙伴可以了解一下2021-12-12C語言詳細(xì)分析宏定義與預(yù)處理命令的應(yīng)用
宏定義是用宏名來表示一個字符串,在宏展開時又以該字符串取代宏名,這只是一種簡單的替換。字符串中可以含任何字符,可以是常數(shù),也可以是表達(dá)式,預(yù)處理程序?qū)λ蛔魅魏螜z查,如有錯誤,只能在編譯已被宏展開后的源程序時發(fā)現(xiàn)2022-07-07