日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

QT設計秒表功能(跑步計時器)_C 語言

作者:Jason~shen ? 更新時間: 2022-09-30 編程語言

本文實例為大家分享了QT設計秒表功能的具體代碼,供大家參考,具體內容如下

設計目標

1. 定時器開始

2.復位從0開始計時

3.記錄--把記錄的時間添加到QTextBrowser, append(時間)

4. QTime t(0,0,0) ? t = t.addMsec( number ) ?t.toString (“hh:mm:ss:zzz”)

定時器(QTimer)的使用

定時器---定時發送信號timeout
QTimer 定時器類

1.創建定時器類對象
QTimer mtimer;

2.把定時器信號與槽函數關聯
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);
? ? //把定時器信號與槽函數關聯
? ? 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();
}

原文鏈接:https://blog.csdn.net/qq_40602000/article/details/97033826

欄目分類
最近更新