一、思维导图
二、练习
头文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTime>
#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);private slots:void on_start_clicked();private:Ui::Widget *ui;int id; //计时器idQString s; //闹钟时间int clock_id; //用于设置闹钟响的次数QTextToSpeech *speecher; //实例化一个播报者};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget), speecher(new QTextToSpeech)
{ui->setupUi(this);id=startTimer(1000);
}Widget::~Widget()
{delete ui;
}void Widget::timerEvent(QTimerEvent *e)
{QTime sys_time = QTime::currentTime();QString s1=sys_time.toString("hh:mm:ss");ui->localtm->setText(s1);ui->label->setAlignment(Qt::AlignCenter);if(s==s1){clock_id=startTimer(10000);ui->label->setText("该起床了!!");speecher->say("该起床了!!");}if(e->timerId()==clock_id){ui->label->setText("迟到了!!");speecher->say("迟到了!!!");}}void Widget::on_start_clicked()
{s = ui->lineEdit->text();ui->mytime->setText("闹钟时间: "+s);
}
Widget 2024-03-26 20-02-49