網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Qt timerEvent實(shí)現(xiàn)簡(jiǎn)單秒表的具體代碼,供大家參考,具體內(nèi)容如下
#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;// 定義定時(shí)器對(duì)象
? ? 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)
{
? ? // 判斷是哪個(gè)定時(shí)器
? ? 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("記錄次數(shù):"+id2s);
}
ui
效果
原文鏈接:https://blog.csdn.net/qq_45827426/article/details/118914284
相關(guān)推薦
- 2022-09-01 一文搞懂Scrapy與MongoDB交互過程_MongoDB
- 2023-01-11 ubuntu20.04虛擬機(jī)無法上網(wǎng)的問題及解決_Linux
- 2022-05-03 Android?Compose自定義TextField實(shí)現(xiàn)自定義的輸入框_Android
- 2022-09-08 Pandas中DataFrame的基本操作之重新索引講解_python
- 2022-07-10 linux 目錄和文件管理
- 2022-11-20 TS?中的類型推斷與放寬實(shí)例詳解_其它
- 2022-07-14 python如何往列表頭部和尾部添加元素_python
- 2022-12-01 SQL?Server主鍵與外鍵設(shè)置以及相關(guān)理解_MsSql
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支