Qt 计时器事件timerEvent 和 计时器类QTimer

#pragma once

#include 
#include "ui_QTimerEventWidge.h"

class QTimerEventWidge : public QWidget
{
	Q_OBJECT

public:
	QTimerEventWidge(QWidget *parent = Q_NULLPTR);
	~QTimerEventWidge();

	virtual void timerEvent(QTimerEvent* event);

private:
	Ui::QTimerEventWidge ui;
};
#include "QTimerEventWidge.h"
#include 

QTimerEventWidge::QTimerEventWidge(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	// 计时器事件timerEvent和计时器类QTimer

	// 开启计时器事件,参数 1000ms 每隔1000ms调用一次timerEvent
	startTimer(1000);
	
	//new 计时器对象
	QTimer* timer = new QTimer(this);
	
	// 计时器信号signal timeout
	connect(timer, &QTimer::timeout, [=](){
		static int num = 0;
		ui.label_2->setText(QString::number(++num));
	});

	// 计时器类必须手动开始
	timer->start(500);

	connect(ui.pushButton, &QPushButton::clicked, [timer]() {
		timer->start(500);
	});

	connect(ui.pushButton_2, &QPushButton::clicked, [timer]() {
		timer->stop();
	});
}

QTimerEventWidge::~QTimerEventWidge()
{
}

void QTimerEventWidge::timerEvent(QTimerEvent * event)
{
	static int timeNumber = 0;
	ui.label->setText(QString::number(++timeNumber));
}

你可能感兴趣的:(Qt,qt,ui,开发语言)