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

學無先后,達者為師

網站首頁 編程語言 正文

Qt?timerEvent實現簡單秒表功能_C 語言

作者:Haragarden ? 更新時間: 2022-10-01 編程語言

本文實例為大家分享了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

欄目分類
最近更新