思维导图
Widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QTime>
#include<QTimerEvent>
#include<QTextToSpeech>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *e)override;private slots:void on_pushButton_clicked();private:Ui::Widget *ui;int id;QTextToSpeech *speecher;bool ok=false;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget), speecher(new QTextToSpeech(this))//初始化语音播报对象
{ui->setupUi(this);id=startTimer(1000);//开启定时器}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_clicked()
{//按钮按下判断按钮上的文本if(ui->pushButton->text()=="启动"){//当按钮上文本为启动时将闹钟启动ui->pushButton->setText("关闭");ok=true;}else{//当按钮上文本为关闭时说明闹钟已经启动,关闭闹钟ui->pushButton->setText("启动");ok=false;}
}
void Widget::timerEvent(QTimerEvent *e)
{if(e->timerId()==id){QTime sys_time=QTime::currentTime();//获取当前系统事件QString t=sys_time.toString("hh:mm:ss");//将系统时间转换为字符串,设置到label中ui->label->setText(t);ui->label->setAlignment(Qt::AlignCenter);//设置label的文本居中if(t==ui->lineEdit->text()&&ok)//当闹钟时间与系统时间相同并且闹钟开启时开始语音播报label_3中的内容{for(int i=0;i<5;i++){speecher->say(ui->label_3->text());}ui->pushButton->setText("开启");//关闭闹钟}}}