#include "mywidget.h"
#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::MyWidget)
{
ui->setupUi(this);
//居中
ui->label02->setAlignment(Qt::AlignCenter);
ui->Edit1->setAlignment(Qt::AlignCenter);
id = startTimer(1000);
}
QString time_str;
void MyWidget::timerEvent(QTimerEvent *e)
{
//显示系统时间
QTime sys_time = QTime::currentTime();
time_str = sys_time.toString("hh:mm:ss");
ui->label02->setText(time_str);
//获取输入的时间,和系统时间比较
QString setTime = ui->Edit1->text();
//时间到则语音播报label文本内容
QTextToSpeech *tts = new QTextToSpeech(this);
if(setTime == time_str)
{
for(int i = 1; i <= 5; i++)
{
tts->say(ui->label01->text());
}
ui->label01->setText("播报完毕,关闭闹钟");
killTimer(id);
}
}
MyWidget::~MyWidget()
{
delete ui;
}
void MyWidget::on_Btn1_clicked()
{
if(ui->Btn1->text()=="启动")
{
ui->Btn1->setText("关闭");
ui->Edit1->setEnabled(false);
ui->label01->setEnabled(false);
}
else
{
killTimer(id);
ui->Btn1->setText("启动");
ui->Edit1->setEnabled(true);
ui->label01->setEnabled(true);
}
}
运行结果: