一、代码实现设置闹钟,到时间后语音提醒用户。示意图如下:
代码:
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget), speecher(new QTextToSpeech(this))
{ui->setupUi(this);//启动一个定时器 每隔一秒执行一次timerEvent函数id = startTimer(1000);
}Widget::~Widget()
{delete ui;
}
//定时器事件函数实现
void Widget::timerEvent(QTimerEvent *e)
{if(e->timerId() == id){//获取系统时间 Qtime类QTime sys_time = QTime::currentTime();//将时间转换成字符串 写入显示时间的Label中ui->systime_label->setText(sys_time.toString("hh:mm:ss"));//居中显示ui->systime_label->setAlignment(Qt::AlignCenter);}else if(e->timerId() == id2){if(ui->systime_label->text() == ui->setclock_Edit->text()){for(int i = 0;i < 3;i++){speecher->say(ui->speak_label->text());}}}
}void Widget::on_start_Btn_clicked()
{//点击启动按钮 设置闹钟id2 = startTimer(1000);
}
运行:
思维导图: