#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->stopBtn->setDisabled(true);
this->setFixedSize(this->size()); //设置固定大小
this->setWindowTitle("简易闹钟");
//实例化一个播报员
speecher = new QTextToSpeech(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_startBtn_clicked()
{
ui->startBtn->setDisabled(true);
ui->alarmEdit->setReadOnly(true);
ui->textEdit->setReadOnly(true);
ui->stopBtn->setEnabled(true);
tId = startTimer(100); //定义一个计时器
}
void Widget::timerEvent(QTimerEvent *e)
{
//判断是哪个计时器就位
if (e->timerId() == tId)
{
QTime sys_time = QTime::currentTime(); //获取系统时间
//将系统时间转换为字符串
QString t = sys_time.toString();
//将字符串展示到label
ui->timeLb->setText(t);
alarmTime = ui->alarmEdit->text(); //获取闹钟时间
//如果到了时间就开始播报
if (t == alarmTime)
{
for (int i = 0; i < 10; i++)
{
speecher->say(ui->textEdit->toPlainText());
}
}
}
}
void Widget::on_stopBtn_clicked()
{
ui->startBtn->setDisabled(false);
ui->alarmEdit->setReadOnly(false);
ui->textEdit->setReadOnly(false);
speecher->stop();
killTimer(tId);
}