Qt?timerEvent實現(xiàn)簡單秒表功能
更新時間:2022年08月05日 15:16:40 作者:Haragarden
這篇文章主要為大家詳細介紹了Qt?timerEvent實現(xiàn)簡單秒表功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Qt timerEvent實現(xià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;// 定義定時器對象 ? ? 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("記錄次數(shù):"+id2s); }
ui
效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
正確理解C++的構(gòu)造函數(shù)和析構(gòu)函數(shù)
在C++的學(xué)習(xí)中,可以把類當(dāng)作一個模具,類實例化出來的對象就是根據(jù)這個模具所產(chǎn)生的實體,對象看作是自己創(chuàng)建的一個新的數(shù)據(jù)類型。本文主要介紹了類對象通過拷貝函數(shù)進行初始化,分析類對象的內(nèi)存模型,以及通過this指針實現(xiàn)更復(fù)雜的功能。最后介紹了析構(gòu)函數(shù)的基礎(chǔ)知識2021-06-06詳解C語言隨機數(shù)設(shè)置的三種方式(保姆級教程)
本篇文章將為大家介紹在C語言中設(shè)置隨機數(shù)的三大方法的使用,文中的示例代碼講解詳細,對我們學(xué)習(xí)C語言有一定的幫助,需要的可以參考一下2022-11-11