網站首頁 編程語言 正文
本文實例為大家分享了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
- 上一篇:Qt編寫秒表功能_C 語言
- 下一篇:QT實現多文件拖拽獲取路徑的方法_C 語言
相關推薦
- 2022-04-25 ASP.NET?Core中Cookie驗證身份用法詳解_實用技巧
- 2022-03-19 nginx?rtmp模塊編譯?arm版本的問題_nginx
- 2023-04-06 C語言中的多行輸入問題及說明_C 語言
- 2021-12-03 Apache?Log4j2?報核彈級漏洞快速修復方法_Linux
- 2023-10-12 img標簽使用base64圖片以及如何將圖片轉為base64格式
- 2022-08-21 jQuery實現簡易商城系統項目實操_jquery
- 2023-02-15 用Python編寫個解釋器實現方法接受_python
- 2023-02-26 React?Fiber原理深入分析_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支