QT設(shè)計(jì)秒表功能(跑步計(jì)時(shí)器)
本文實(shí)例為大家分享了QT設(shè)計(jì)秒表功能的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)計(jì)目標(biāo)
1. 定時(shí)器開(kāi)始
2.復(fù)位從0開(kāi)始計(jì)時(shí)
3.記錄--把記錄的時(shí)間添加到QTextBrowser, append(時(shí)間)
4. QTime t(0,0,0) t = t.addMsec( number ) t.toString (“hh:mm:ss:zzz”)
定時(shí)器(QTimer)的使用
定時(shí)器---定時(shí)發(fā)送信號(hào)timeout
QTimer 定時(shí)器類(lèi)
1.創(chuàng)建定時(shí)器類(lèi)對(duì)象
QTimer mtimer;
2.把定時(shí)器信號(hào)與槽函數(shù)關(guān)聯(lián)
connect(&mtimer, &QTimer::timeout, this, &TimerWin::on_outBt_clicked);
3.啟動(dòng)定時(shí)器
mtimer.start(1000);
4.停止定時(shí)器
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í)器信號(hào)與槽函數(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()<<"啟動(dòng)定時(shí)器";
? ? mtimer.start(10);
}
?
void StopwatchWin::on_stopBtn_clicked()
{
? ? qDebug()<<"停止定時(shí)器";
? ? 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();
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++的cout.tellp()和cout.seekp()語(yǔ)法介紹
無(wú)論是使用 cout 輸出普通數(shù)據(jù),用 cout.put() 輸出指定字符,還是用 cout.write() 輸出指定字符串,數(shù)據(jù)都會(huì)先放到輸出流緩沖區(qū),待緩沖區(qū)刷新,數(shù)據(jù)才會(huì)輸出到指定位置,本文給大家介紹一下C++的cout.tellp()和cout.seekp()語(yǔ)法,需要的朋友可以參考下2023-09-09
C語(yǔ)言中const和define的區(qū)別你了解嘛
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言中const和define的區(qū)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
iostream與iostream.h的區(qū)別詳細(xì)解析
以下是對(duì)C++中iostream與iostream.h的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-09-09
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中串的模式匹配
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中串的模式匹配的相關(guān)資料,需要的朋友可以參考下2017-05-05
C語(yǔ)言中查詢(xún)進(jìn)程信號(hào)是否被遮罩或擱置的簡(jiǎn)單方法
這篇文章主要介紹了C語(yǔ)言中查詢(xún)進(jìn)程信號(hào)是否被遮罩或擱置的簡(jiǎn)單方法,包括sigprocmask函數(shù)和sigpending函數(shù)的簡(jiǎn)介,需要的朋友可以參考下2015-09-09
C語(yǔ)言中字符串與各數(shù)值類(lèi)型之間的轉(zhuǎn)換方法
這篇文章主要介紹了C語(yǔ)言中字符串與各數(shù)值類(lèi)型之間的轉(zhuǎn)換方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Qt實(shí)現(xiàn)導(dǎo)出QTableWidget/QTableView數(shù)據(jù)
這篇文章主要介紹了在Qt中實(shí)現(xiàn)將QTableWidget或者QTableView中的數(shù)據(jù)直接導(dǎo)出的示例代碼,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-01-01
C++?STL之string的模擬實(shí)現(xiàn)實(shí)例代碼
C++中有命名空間的存在,我們只需把我們的代碼封到自定義的命名空間即可,下面這篇文章主要給大家介紹了關(guān)于C++?STL之string的模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-01-01

