实现函数
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
startTimer(1000);
this->setWindowTitle("闹钟");
this->setWindowIcon(QIcon(":/icon/clock.png"));
this->setWindowOpacity(0.8);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *event)
{
if(timeId == event->timerId())
{
//如果到达设定时间播报通知内容
//实例化一个语音播报者对象
QTextToSpeech *speech = new QTextToSpeech;
//获取通知内容
QString text = ui->notify->toPlainText();
if(sysTime.toString("hh:mm") == clock)
{
speech->say(text);
}
}
//将系统时间展示到ui界面
//获取系统时间
sysTime = QTime::currentTime();
//将当前时间转换为字符串
QString time = "当前时间:"+sysTime.toString("hh:mm:ss");
//将字符串展示到ui界面中
ui->sysTime->setText(time);
}
//启动按钮对应的槽函数
void Widget::on_start_clicked()
{
//获取设定时间
clock = ui->clock->text();
//启动一个定时器,每隔一秒判断是否达到设定时间
timeId = startTimer(1000);
//将启动按钮、行编辑器、文本编辑器都设为不可用状态
ui->start->setEnabled(false);
ui->clock->setEnabled(false);
ui->notify->setEnabled(false);
}
//取消按钮对应的槽函数
void Widget::on_cancel_clicked()
{
//当已设置闹钟后设置弹窗
if(clock != nullptr)
{
QMessageBox msgbox(QMessageBox::Question, "Question", "您是否要取消设定的闹钟",QMessageBox::Yes | QMessageBox::No);
int res = msgbox.exec();
//结果如果为yes,则将闹钟取消,并将编辑器和启动按钮设为可用
if(res == QMessageBox::Yes)
{
clock = nullptr;
killTimer(timeId);
ui->start->setEnabled(true);
ui->clock->setEnabled(true);
ui->notify->setEnabled(true);
}
}
//未设置闹钟时,点击取消会将编辑器内容清空
else
{
ui->clock->setText(nullptr);
ui->notify->setText(nullptr);
}
}
功能说明
闹钟最多会提醒1分钟,提醒内容可以自己设置,点击启动则将闹钟设定完成,取消在未设定闹钟时可以将所填内容清空,设定之后可以取消闹钟
测试结果