widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QTimer>
#include <QLabel>
#include <QLineEdit>
#include <QTextToSpeech>
#include <QVoice>
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_startBtn_clicked();
void on_stopBtn_clicked();
private:
Ui::Widget *ui;
int tId;
QTextToSpeech *speecher;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// tId=startTimer(1000);
ui->lineEdit->setPlaceholderText("闹钟时间");
ui->textEdit->setText("太阳晒屁股了bro");
ui->lineEdit->setAlignment(Qt::AlignCenter);
ui->textEdit->setAlignment(Qt::AlignCenter);
speecher=new QTextToSpeech(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget:: timerEvent(QTimerEvent *e)
{
if(e->timerId() == tId)
{
QTime sys_time = QTime::currentTime(); //获取当前系统时间
QString s = sys_time.toString("hh::mm::ss");//把系统时间转换成字符串
ui->systemTimeLab->setText(s);//将系统时间放入标签中
ui->systemTimeLab->setAlignment(Qt::AlignCenter);//居中显示
if(ui->lineEdit->text()==ui->systemTimeLab->text())
{
speecher->say(ui->textEdit->toPlainText());
}
}
}
void Widget::on_startBtn_clicked()
{
tId=startTimer(1000);
}
void Widget::on_stopBtn_clicked()
{
this->killTimer(tId);
}