头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTime>//时间类
#include<QTimerEvent>//定时器事件类
#include<QtTextToSpeech> //语言播报类
#include<QPushButton>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *e);//定时器事件函数的重写声明
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int id;
//创建一个语言播报者
QTextToSpeech *speecher;
int temp=0;
};
#endif // WIDGET_H
源文件:
#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);
//给语音播报者实例化空间
speecher = new QTextToSpeech(this);
}
Widget::~Widget()
{
delete ui;
}
//当定时器超时时,自动执行这个函数
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId()==id)
{
//获取系统时间
QTime sys_time = QTime::currentTime();
//将类型转换
QString s = sys_time.toString("hh:mm:ss");
//将系统时间放入ui界面的label中
ui->label->setText(s);
if(1==temp&&ui->lineEdit->text()==ui->label->text())
{
for(int i=0;i<5;i++)
{
speecher->say(ui->label_3->text());
}
}
//居中显示
ui->label->setAlignment(Qt::AlignCenter);
}
}
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text()=="启动")
{
temp=1;
ui->pushButton->setText("关闭");
}
else {
temp=0;
//killTimer(id);
ui->pushButton->setText("启动");
}
}