#include "widget.h"
#include "ui_widget.h"
//需要在.pro文件第一行加 texttospeech
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->lineEdit->setPlaceholderText("时:分:秒");//设定框占位字符
//ui->textEdit->setText("春眠不觉晓");
ui->lineEdit->setAlignment(Qt::AlignCenter);//居中显示
ui->textEdit->setAlignment(Qt::AlignCenter);
speecher=new QTextToSpeech(this);//给播报员分配空间
}
Widget::~Widget()
{
delete ui;
}
void Widget:: timerEvent(QTimerEvent *e)
{
if(e->timerId() == tId)
{
QTime sys_time = QTime::currentTime(); //获取当前系统时间
QString s = sys_time.toString("hh:mm:ss");//把系统时间转换成字符串
ui->lab_time->setText(s);//将系统时间放入标签中
ui->lab_time->setAlignment(Qt::AlignCenter);//居中显示
if(s == ui->lineEdit->text())//判断设定时间与系统时间是否匹配
{
speecher->say(ui->textEdit->toPlainText());
}
}
}
void Widget::on_startBtn_clicked()//定时器启动事件
{
tId=startTimer(1000);
}
void Widget::on_stopBtn_clicked()//定时器关闭事件
{
this->killTimer(tId);
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QTimer>
#include <QLabel>
#include <QLineEdit>
#include <QTextToSpeech>
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_startBtn_clicked();
void on_stopBtn_clicked();
private:
Ui::Widget *ui;
int tId;
QTextToSpeech *speecher;
};
#endif // WIDGET_H