作业:
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include<QPushButton>
#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_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::Widget *ui;//定时器idint id;//实例化一个语音播报者QTextToSpeech *speecher;
};
#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.h
#include "widget.h"
#include "ui_widget.h"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::timerEvent(QTimerEvent *e){//获取当前系统时间QTime t=QTime::currentTime();//将系统时间转换成字符串QString s=t.toString("h:m:s");//将系统时间放入lab中ui->label->setText(s);ui->label->setAlignment(Qt::AlignCenter);if(s==ui->lineEdit->text()){for(int i=0;i<5;i++){// QString str=ui->lineEdit_2->text();ui->label_3->setText("起床");speecher->say(ui->label_3->text());}}}//按钮的槽函数
void Widget::on_pushButton_clicked()
{//启动一个定时器id=startTimer(1000);
}void Widget::on_pushButton_2_clicked()
{//关闭定时器killTimer(id);this->close();
}