Qt timerEvent秒表功能
Haragarden 人气:0#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
效果
加载全部内容