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中顯示的時間進(jìn)行更新,即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ù)中進(jìn)行初始化 ?
?? ?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++利用多態(tài)實現(xiàn)職工管理系統(tǒng)(項目開發(fā))
這篇文章主要介紹了C++利用多態(tài)實現(xiàn)職工管理系統(tǒng)(項目開發(fā)),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
C++中左值引用,右值引用,萬能引用的關(guān)系及區(qū)別說明
這篇文章主要介紹了C++中左值引用,右值引用,萬能引用的關(guān)系及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
Linux下C語言的幾道經(jīng)典面試題小結(jié)(分享)
下面小編就為大家?guī)硪黄狶inux下C語言的幾道經(jīng)典面試題小結(jié)(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

