1.思维导图
2.完成闹钟的实现
头文件
#include <QTextToSpeech>
#include <QTextEdit>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTQLineEdit *edit1=new QLineEdit;// QTextEdit *edit2=new QTextEdit;public:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *e);private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::Widget *ui;QTextToSpeech *speecher;int tId;
};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);speecher =new QTextToSpeech(this);}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_clicked()
{if(ui->pushButton->text()=="开始"){tId=startTimer(1000);}
}void Widget::on_pushButton_2_clicked()
{if(ui->pushButton_2->text()=="停止"){this->killTimer(tId);}
}void Widget::timerEvent(QTimerEvent *e)
{if(e->timerId()==tId){QTime sys_time=QTime::currentTime();QString t=sys_time.toString("hh-mm-ss");ui->label_2->setText(t);ui->label_2->setAlignment(Qt::AlignCenter);QString inputTime=ui->lineEdit->text();QTime alarmTime=QTime::fromString(inputTime,"hh-mm-ss");ui->lineEdit->setAlignment(Qt::AlignCenter);if(alarmTime== sys_time){QString speechText=ui->textEdit->toPlainText();speecher->say(speechText);}}}