#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#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_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
int id;
QTextToSpeech *speecher;
};
#endif // WIDGET_H
#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->label_2->setText("请输入要定闹钟的时间");
ui->label_3->setText("好好学习,天天向上");
}
Widget::~Widget()
{
delete ui;
}
void Widget:: timerEvent(QTimerEvent *e)
{
if(e->timerId() == id)
{
//获取系统时间
QTime sys_time =QTime::currentTime();
//时间转换成字符串
//系统时间放入label
ui->label->setText(sys_time.toString("hh::mm::ss"));
ui->label->setAlignment(Qt::AlignCenter);
}
}
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "启动")
{
id = startTimer(1000);//毫秒
ui->pushButton->setText("关闭");
}
else
{
killTimer(id);
ui->pushButton->setText("启动");
}
}
void Widget::on_pushButton_2_clicked()
{
if(ui->label->text() == ui->lineEdit->text())
{
for(int i=0;i<5;i++)
{
speecher->say(ui->lineEdit->text());
}
}
}