.h代码
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
- void timerEvent(QTimerEvent *event) override;
- int t1;
- int t2;
-
- private slots:
- void on_pushButton_clicked();
-
- private:
- Ui::Widget *ui;
- QTextToSpeech *speech;
- };
- #endif // WIDGET_H
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
- t1=startTimer(1000);
- speech = new QTextToSpeech(this);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::timerEvent(QTimerEvent *e)
- {
- if(e->timerId()==t1)
- {
- QTime sys_time = QTime::currentTime();
- QString s=sys_time.toString("hh:mm:ss");
- ui->label->setText(s);
- }
- if(e->timerId()==t2)
- {
- for (int i=0;i<5;i++)
- {
- ui->label_3->setText("好好学习天天向上");
- speech->say(ui->label_3->text());
- }
- killTimer(t2);
- }
- }
-
-
- void Widget::on_pushButton_clicked()
- {
- QTime sys_time =QTime::currentTime();
- QTime alarm_time=ui->timeEdit->time();
- int current_s=sys_time.hour()*3600+sys_time.minute()*60+sys_time.second();
- int alarm_s=alarm_time.hour()*3600+alarm_time.minute()*60+alarm_time.second();
- t2=startTimer((alarm_s-current_s)*1000);
- }
效果图