示例:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
t1=new QTimer(this);
ui->closeButton->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
//启动按钮
void MainWindow::on_stratButton_clicked()
{
//启动一个定时器
tid=this->startTimer(1000);
ui->stratButton->setEnabled(false);
ui->closeButton->setEnabled(true);
}
//关闭按钮
void MainWindow::on_closeButton_clicked()
{
//关闭一个定时器
this->killTimer(tid);
ui->stratButton->setEnabled(true);
ui->closeButton->setEnabled(false);
}
//定时器事件处理函数
void MainWindow::timerEvent(QTimerEvent *e)
{
//获取系统当前时间
QTime sys_time=QTime::currentTime();
//将Qtime类型转换为字符串类型
QString time=sys_time.toString("hh:mm:ss");
//将label内容居中
ui->label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
//将时间放在ui上的label中显示
ui->label->setText(time);
//判断时间是否相等
if(time==ui->timeEdit->text())
{
//如果相等打印要说的话
ui->msglabel->setText("今天二狗约着撸串,别忘了准时出发");
}
}