作业要求:
代码运行效果图:
代码:
头文件:
#ifndef ALARMCLOCK_H
#define ALARMCLOCK_H#include <QWidget>
#include <QTimerEvent>
#include <QTimer>
#include <QTime>
#include <QTextToSpeech>QT_BEGIN_NAMESPACE
namespace Ui { class AlarmClock; }
QT_END_NAMESPACEclass AlarmClock : public QWidget
{Q_OBJECTpublic:AlarmClock(QWidget *parent = nullptr);~AlarmClock();void timerEvent(QTimerEvent *e);private slots:void on_sbtn_clicked();void on_cbtn_clicked();private:Ui::AlarmClock *ui;int id;QTextToSpeech *speech;
};
#endif // ALARMCLOCK_H
源文件:
#include "alarmclock.h"
#include "ui_alarmclock.h"AlarmClock::AlarmClock(QWidget *parent): QWidget(parent), ui(new Ui::AlarmClock)
{ui->setupUi(this);speech = new QTextToSpeech(this);
}AlarmClock::~AlarmClock()
{delete ui;
}void AlarmClock::timerEvent(QTimerEvent *e)
{//获取系统时间QTime systime = QTime::currentTime();//将系统时间转换QString s = systime.toString("hh:mm:ss");//将转换后的系统时间放入Label中ui->timelab->setText(s);//居中显示ui->timelab->setAlignment(Qt::AlignCenter);//比对闹钟时间和系统时间,如果相等,则播报闹钟if(ui->tedit->text() == s){for(int i=0;i<3;i++){speech->say(ui->speechlab->text());}}
}//启动按钮点击信号对应的槽函数
void AlarmClock::on_sbtn_clicked()
{//打开一个定时器id = startTimer(1000);}//关闭按钮点击信号对应的槽函数
void AlarmClock::on_cbtn_clicked()
{killTimer(id);
}
主函数:
#include "alarmclock.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);AlarmClock w;w.show();return a.exec();
}