思维导图
项目文件里面要加texttospeech模块
widget.h:
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTime>//时间类
#include <QTextToSpeech>//语音播报类QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();//重写定时器事件虚函数void timerEvent(QTimerEvent *e);private slots:void on_pushButton_clicked();private:Ui::Widget *ui;QTextToSpeech *speecher;//实例化一个语音播报者int id;//定义定时器的id号int id2;QString t;
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget), speecher(new QTextToSpeech(this))//给语音播报者实例化分配空间
{ui->setupUi(this);id = startTimer(1000);//启动一个定时器
}Widget::~Widget()
{delete ui;
}void Widget::timerEvent(QTimerEvent *e)//定时器超时,自动执行的功能函数
{static int num = 0;if(e->timerId() == id)//判断哪个定时器超时{//获取系统时间QTime sys_time = QTime::currentTime();//将时间转换成字符串t = sys_time.toString("hh-mm-ss");//将系统时间放入标签1中ui->label->setText(t);//设置标签里面的字体大小ui->label->setStyleSheet("font:22px");//让标签文本居中显示ui->label->setAlignment(Qt::AlignCenter);}else if(e->timerId() == id2){ui->lineEdit->setStyleSheet("font:22px");if(ui->lineEdit->text() == t)//比较行编辑器里的时间是否与系统时间相等{while(num != 5){speecher->say(ui->label_3->text());//语音播报者播报标签里的文本内容num++;}}}
}//按钮对应的槽函数
void Widget::on_pushButton_clicked()
{if(ui->pushButton->text() == "启动"){//启动另一个定时器id2 = startTimer(1000);//修改按钮文本ui->pushButton->setText("取消");}else{//关闭定时器killTimer(id2);//修改按钮文本ui->pushButton->setText("启动");}
}