利用QT設(shè)計(jì)秒表功能
本文實(shí)例為大家分享了QT設(shè)計(jì)秒表功能的具體代碼,供大家參考,具體內(nèi)容如下
一、窗口界面的設(shè)置
可以使用屬性欄digitCount來(lái)設(shè)計(jì)此時(shí)0的初始位置
二、代碼的編寫
1、確定時(shí)間->每間隔一秒調(diào)用一個(gè)update函數(shù)(進(jìn)行換算)或者直接調(diào)用QT里面的time類;
2、確定顯示的方式;
3、每間隔一段時(shí)間調(diào)用顯示來(lái)達(dá)到動(dòng)態(tài)效果;
4、這里應(yīng)該查看Qtime/Qlcd的help手冊(cè)
5、在對(duì)定時(shí)器聲明時(shí)要注意需要聲明成指針,后面的槽函數(shù)發(fā)出信號(hào)者要求是指針類型;
6、利用基準(zhǔn)時(shí)間與當(dāng)前時(shí)間的差值來(lái)顯示計(jì)時(shí)器;
7、設(shè)置暫停鍵的時(shí)候需要注意的是,可以給按鍵設(shè)置兩個(gè)功能,分別是暫停和繼續(xù)形成一個(gè)循環(huán),但是需要注意是,當(dāng)我們暫停后再繼續(xù)這段時(shí)間也是計(jì)算在內(nèi)的,當(dāng)再次顯示的時(shí)候,這時(shí)將會(huì)出現(xiàn)跳躍,這是因?yàn)槲覀冿@示時(shí)間的時(shí)候用的是當(dāng)前時(shí)間與基準(zhǔn)時(shí)間的差值來(lái)計(jì)算的。解決的辦法是:
獲取暫停前和暫停后的值,保存下來(lái),當(dāng)再次開始計(jì)算時(shí)減去這部分時(shí)間即可。
8、還有注意設(shè)置每個(gè)按鍵的可用性(Enabled)
代碼如下:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <Qtimer> #include <Qtime> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { ? ? Q_OBJECT public: ? ? explicit MainWindow(QWidget *parent = 0); ? ? ~MainWindow(); private slots: ? ? void updateTimeAndDisplay(); ? ?//槽函數(shù) ? ? void on_btn_start_clicked(); ? ? void on_btn_stop_clicked(); ? ? void on_btn_pause_clicked(); ? ? void on_btn_log_clicked(); private: ? ? Ui::MainWindow *ui; ? ? QTimer *qtimer; ? ? ? ? ? ? ? ? //聲明一個(gè)定時(shí)器對(duì)象,聲明成指針對(duì)象 ? ? QTime baseTime; ? ? ? ? ? ? ? ? //聲明一個(gè)時(shí)間對(duì)象 ? ? QString showStr; ? ? ? ? ? ? ? ?//直接聲明 }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QString> MainWindow::MainWindow(QWidget *parent) : ? ? QMainWindow(parent), ? ? ui(new Ui::MainWindow) { ? ? ui->setupUi(this); ? ? this->qtimer = new QTimer; ? //實(shí)例構(gòu)造 ? ? connect(this->qtimer,SIGNAL(timeout()),this,SLOT(updateTimeAndDisplay())); } MainWindow::~MainWindow() { ? ? delete ui; } //計(jì)算基準(zhǔn)時(shí)間和當(dāng)前時(shí)間的差值來(lái)顯示秒表值(currentTime) void MainWindow::updateTimeAndDisplay() { ? ? QTime current = QTime::currentTime(); ? ? int temp = this->baseTime.msecsTo(current); ? //計(jì)算差值 ? ? QTime showTime(0,0,0,0); ? ? showTime = showTime.addMSecs(temp); ? ? showStr = showTime.toString("hh:mm:ss:zzz"); ?//時(shí)間轉(zhuǎn)換為字符串 ? ? this->ui->lcdNumber->display(showStr); } void MainWindow::on_btn_start_clicked() { ? ? this->baseTime = QTime::currentTime(); ? ? this->qtimer->start(1); ? ? this->ui->btn_start->setEnabled(false); ? ?//設(shè)置按鍵的可用性 } void MainWindow::on_btn_stop_clicked() { ? ? if(this->ui->btn_stop->text() == "停止"){ ? ? ? ? this->ui->btn_stop->setText("清零"); ? ? ? ? this->qtimer->stop(); ? ? ? ? this->ui->btn_start->setEnabled(false); ? ? ? ? this->ui->btn_pause->setEnabled(false); ? ? }else ? ? { ? ? ? ? this->ui->lcdNumber->display("00:00:00:000"); ? ? ? ? this->ui->textBrowser->clear(); ? ? ? ? this->ui->btn_stop->setText("停止"); ? ? ? ? this->ui->btn_start->setEnabled(true); ? ? ? ? this->ui->btn_pause->setEnabled(true); ? ? } } //獲取暫停前后的值求差值 void MainWindow::on_btn_pause_clicked() { ? ? static QTime pauseTime; ? ? if(this->ui->btn_pause->text() == "暫停"){ ? ? ? ? pauseTime = QTime::currentTime(); ? ? ? ? this->qtimer->stop(); ? ? ? ? this->ui->btn_pause->setText("繼續(xù)"); ? ? ? ? this->ui->btn_start->setEnabled(false); ? ? ? ? this->ui->btn_stop->setEnabled(false); ? ? }else ? ? { ? ? ? ? QTime cut = QTime::currentTime(); ? ? ? ? int t = pauseTime.msecsTo(cut); ? ? ? ? //求差值 ? ? ? ? this->baseTime = baseTime.addMSecs(t); ?//把暫停時(shí)間再給baseTime ? ? ? ? this->qtimer->start(1); ? ? ? ? this->ui->btn_pause->setText("暫停"); ? ? ? ? this->ui->btn_start->setEnabled(true); ? ? ? ? this->ui->btn_stop->setEnabled(true); ? ? } } void MainWindow::on_btn_log_clicked() { ? ? this->ui->textBrowser->append(showStr); //這里如果使用settext()將會(huì)覆蓋信息,不能追加顯示 }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言字符串左旋的兩種實(shí)現(xiàn)方法
匯編語(yǔ)言中有一種移位指令叫做循環(huán)左移(ROL),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言字符串左旋的兩種實(shí)現(xiàn)方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02C語(yǔ)言 volatile與const同時(shí)使用應(yīng)注意的問題
“volatile”的含義是“請(qǐng)不要做沒譜的優(yōu)化,這個(gè)值可能變掉的”,而并非“你可以修改這個(gè)值”。因此,它們本來(lái)就不是矛盾的2013-09-09c++實(shí)現(xiàn)對(duì)輸入數(shù)組進(jìn)行快速排序的示例(推薦)
下面小編就為大家?guī)?lái)一篇c++實(shí)現(xiàn)對(duì)輸入數(shù)組進(jìn)行快速排序的示例(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-06-06Qt項(xiàng)目實(shí)戰(zhàn)之方塊游戲的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了如何利用Qt實(shí)現(xiàn)簡(jiǎn)易的方塊游戲,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下2023-03-03C語(yǔ)言編程中常見的五種錯(cuò)誤及對(duì)應(yīng)解決方案
這篇文章主要給大家分享的是C語(yǔ)言編程中常見的五種錯(cuò)誤及對(duì)應(yīng)解決方案,詳細(xì)內(nèi)容就請(qǐng)跟小編一起進(jìn)入下面的文章內(nèi)容吧2021-10-10VS2022新建項(xiàng)目時(shí)沒有ASP.NET Web應(yīng)用程序(.NET Framework)
本文主要介紹了VS2022新建項(xiàng)目時(shí)沒有ASP.NET Web應(yīng)用程序的解決,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10