網站首頁 編程語言 正文
本文實例為大家分享了Qt timerEvent實現簡單秒表的具體代碼,供大家參考,具體內容如下
#ifndef WIDGET_H
#define WIDGET_H
//頭文件
#include<QWidget>
#include<QObject>
#include<QTimerEvent>
#include<QTimer>
?
namespace Ui {
class Widget;
}
?
class Widget : public QWidget
{
? ? Q_OBJECT
?
public:
? ? explicit Widget(QWidget *parent = 0);
? ? ~Widget();
? ? double i = 0;
? ? //QString s = QString::number(i);
?
private:
? ? Ui::Widget *ui;
? ? //QTimer *myTimer;// 定義定時器對象
? ? int id1,id3;
? ? int id2 = 0;
?
protected:
? ? void timerEvent(QTimerEvent *event); // 聲明
?
private slots:
? ? void on_pushButton_clicked();
? ? void on_pushButton_3_clicked();
? ? void on_pushButton_2_clicked();
};
?
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<QObject>
#include<QTimer>
#include<QDebug>
?
Widget::Widget(QWidget *parent) :
? ? QWidget(parent),
? ? ui(new Ui::Widget)
{
? ? ui->setupUi(this);
? ? ui->lineEdit->setFixedHeight(60);
? ? ui->lineEdit->setFont(QFont( "Arial" , 32 ));
? ? ui->lineEdit->setText("0.0");
?
}
?
Widget::~Widget()
{
? ? delete ui;
}
?
?
void Widget::timerEvent(QTimerEvent *event)
{
? ? // 判斷是哪個定時器
? ? if(event->timerId() == id1){
? ? ? ? qDebug() << "timer1";
? ? ? ? i = i+0.1;
? ? ? ? QString s = QString::number(i);
? ? ? ? ui->lineEdit->setText(s);
? ? }
?
}
void Widget::on_pushButton_clicked() ?//開始
{
? ? id1 = startTimer(100);
? ? //timer_1 = startTimer(100);
?
}
?
void Widget::on_pushButton_3_clicked() ?//停止
{
? ? killTimer(id1);
}
?
void Widget::on_pushButton_2_clicked() ?//記錄
{
? ? QString s = ui->lineEdit->text();
? ? ui->listWidget->addItem(s);
? ? id2=id2+1;
? ? QString id2s = QString::number(id2);
? ? ui->label_2->setText("記錄次數:"+id2s);
}
ui
效果
原文鏈接:https://blog.csdn.net/qq_45827426/article/details/118914284
相關推薦
- 2022-06-20 C語言手把手帶你掌握帶頭雙向循環鏈表_C 語言
- 2022-06-22 Android使用http實現注冊登錄功能_Android
- 2022-09-29 C++Vector容器常用函數接口詳解_C 語言
- 2022-08-25 Asp.net?core中依賴注入的實現_實用技巧
- 2022-03-05 centos7下搭建DNS服務器介紹_Linux
- 2022-02-19 小程序監聽屏幕滾動 判斷滾動方向上下滾動且滾動到一定位置
- 2022-05-01 淺談Redis哨兵模式高可用解決方案_Redis
- 2023-03-19 C語言利用goto語句設計實現一個關機程序_C 語言
- 最近更新
-
- 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同步修改后的遠程分支