1.头文件:
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTime>//时间类
#include <QTimer>//时间事件类
#include <QTimerEvent>//定时器类
#include <QTextToSpeech>
namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *e) override;//重新声明
private slots:void on_pushButton_clicked();private:Ui::Widget *ui;int id;//定义定时器idint temp;//全局变量QString sectime;QTextToSpeech *speecher;
};#endif // WIDGET_H
源文件:widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);id = startTimer(1000);//开启定时器ui->txtlab->setAlignment(Qt::AlignCenter);//居中ui->timeoutlab->setAlignment(Qt::AlignCenter);speecher = new QTextToSpeech(this);//给speecher申请空间
}Widget::~Widget()
{delete ui;
}void Widget::timerEvent(QTimerEvent *e)//重写该函数
{if(e->timerId()==id){QTime s = QTime::currentTime();//获取当前时间QString systime = s.toString("hh::mm::ss");//将时间转化成字符串ui->systimelabel->setText(systime);//将时间显示ui->systimelabel->setAlignment(Qt::AlignCenter);//居中if((ui->timelineEdit->text())==systime&&(temp == 1))//判断时间字符串和行编辑器的是否相等且temp为1{int i;for(i = 0; i < 5;i++)//循环5次{speecher->say(ui->timeoutlab->text());}}}}void Widget::on_pushButton_clicked()
{if(ui->pushButton->text()=="启动"){temp = 1;//将temp设置为1ui->pushButton->setText("关闭");//改变按钮文本}else {temp = 0;ui->pushButton->setText("启动");}
}
效果图
2.思维导图