widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QTimerEvent>
#include <QTime>
#include <QTextToSpeech>
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 *e);
private slots:
void on_time_linkActivated(const QString &link);
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int id;
QString time;
QTextToSpeech *speecher;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
,speecher(new QTextToSpeech(this))
{
ui->setupUi(this);
ui->lineEdit->text();
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == id){
//获取系统时间
QTime winTime = QTime::currentTime();
//系统时间转换为字符串
time = winTime.toString("hh:mm:ss");
//将系统时间放入lebel中
ui->time->setText(time);
}
if(ui->lineEdit->text() == time)
{
for (int i = 0;i < 5;i++) {
speecher->say(ui->label->text());
}
}
}
void Widget::on_time_linkActivated(const QString &link)
{
}
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "启动")
{
id = startTimer(1000);
ui->pushButton->text() = "取消";
}
else if(ui->pushButton->text() == "取消")
{
killTimer(id);
ui->pushButton->text() = "启动";
}
}