#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QtTextToSpeech/QTextToSpeech>#include <QIcon>#include <QTimer>#include <QTime>#include <QTimerEvent>QT_BEGIN_NAMESPACEnamespace Ui { class Widget; }QT_END_NAMESPACEclass Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *event);private slots:void on_startbtn_clicked();void on_stopbtn_clicked();private:Ui::Widget *ui;int sys_timeID;int set_timeID;};#endif // WIDGET_H
#include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){ui->setupUi(this);this->setWindowTitle("事务提醒神器");this->setWindowIcon(QIcon("C:\\Users\\Administrator\\Desktop\\icon\\clock_icon.png"));sys_timeID = startTimer(100);}Widget::~Widget(){delete ui;}void Widget::timerEvent(QTimerEvent *event){if(event->timerId() == sys_timeID){QDateTime sys_time = QDateTime::currentDateTime();QString datetime = sys_time.toString("yyyy年MM月dd日 hh:mm:ss");ui->systemdatetime->setText(datetime);}else if(event->timerId() == set_timeID){if(ui->systemdatetime->text() == ui->settim->text()){QTextToSpeech *sound = new QTextToSpeech(this);QString read_text = ui->sound_text->toPlainText();sound->say(read_text);killTimer(set_timeID);ui->settim->setReadOnly(false);ui->sound_text->setReadOnly(false);}}}void Widget::on_startbtn_clicked(){set_timeID = startTimer(900);ui->settim->setReadOnly(true);ui->sound_text->setReadOnly(true);}void Widget::on_stopbtn_clicked(){killTimer(set_timeID);ui->settim->setReadOnly(false);ui->sound_text->setReadOnly(false);}