day3_QT
- 1、文件保存
- 2、始终事件 -闹钟
1、文件保存
2、始终事件 -闹钟
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#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 *event) override;private slots:void on_startBtn_clicked();void on_overBtn_clicked();private:Ui::Widget *ui;int timer_id_show; //定时器的id号int timer_id_start;QString inputTime;QTextToSpeech *speecher;};
#endif // WIDGET_H
widget.c
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{//启动时的定时器时间 用于左上时间的显示timer_id_show = this->startTimer(1000);speecher = new QTextToSpeech(this);ui->setupUi(this);
}//定时器事件处理
void Widget::timerEvent(QTimerEvent *event)
{QString timeStr;if(event->timerId() == timer_id_show){/*ui界面优化*/ui->timeLabel->setAlignment(Qt::AlignCenter);//启动程序时候,就要显示表QTime sys_t = QTime::currentTime();timeStr = sys_t.toString("hh:mm:ss");ui->timeLabel->setText(timeStr);}if(event->timerId() == timer_id_start){//处理时间相同否?if(inputTime == timeStr ){//时间到QString str = ui->text->toPlainText();speecher->say(str);}}
}void Widget::on_startBtn_clicked()
{inputTime = ui->inTime->text();timer_id_start = this->startTimer(1000);
}void Widget::on_overBtn_clicked()
{this->killTimer(timer_id_start);ui->inTime->clear();
}Widget::~Widget()
{this->killTimer(timer_id_show);delete ui;
}