一、作业
1.Widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimerEvent> //定时器类
#include <QTime>
#include <QtTextToSpeech> //文本转语音类QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *e);
signals:void my_signal(); //定义一个信号,只要声明,不用实现
private slots:void on_startBtn_clicked();private:Ui::Widget *ui;int id;QTextToSpeech *speecher; //实例化一个语音播报者
};
#endif // WIDGET_H
2.Widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget),speecher(new QTextToSpeech(this))
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}//重写定时器事件函数的实现
void Widget::timerEvent(QTimerEvent *e)
{if(e->timerId() == id){QTime sys_time = QTime::currentTime();QString t = sys_time.toString("hh:mm:ss");//将系统时间放入lab中ui->timeLab->setText(t);if( t == ui->timerEdit->text()) //当填入的时间等于实际时间时,闹钟播报sayLab内的内容{for(int num = 5; num > 0; num--) //到了闹钟定的时间时,播报5次停止{speecher->say(ui->sayLab->text());}}}
}//点击启动后运行的槽函数
void Widget::on_startBtn_clicked()
{if(ui->startBtn->text() == "启动"){id = startTimer(1000);ui->timeLab->setAlignment(Qt::AlignCenter);}}
个人效果图:
二、思维导图