widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
tiid=startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()//启动按钮
{
ui->textEdit->setEnabled(false); //设置文本框不可输入
tid=startTimer(500);
}
void Widget::timerEvent(QTimerEvent *e)//定时函数
{
if(e->timerId()==tid)
{
if(QString::compare(ui->lineEdit->text(),ui->lineEdit_2->text(), Qt::CaseInsensitive)==0)//比较文本内容
{
//语音播报
QTextToSpeech *tts = new QTextToSpeech(this);
// tts ->setRate(0.3);
// tts ->setPitch(0.5);
// tts ->setVolume(1);
tts->say(ui->textEdit->toPlainText());
qDebug()<<1;
}
}else
{
//文本框中显示时间
QTime ltime=QTime::currentTime();
QString t = ltime.toString("hh-mm-ss");
ui->lineEdit->setText(t);
ui->textEdit->setPlaceholderText("请输入提示内容..."); //设置占位符
}
}
void Widget::on_pushButton_2_clicked()//取消播报函数
{
ui->textEdit->setEnabled(true);
this->killTimer(tid);
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QTextToSpeech>
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *e);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
int tid;
int tiid;
};
#endif // WIDGET_H