#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include<QMouseEvent>
#include<QTextToSpeech>
#include<QTime>
#include<QTimerEvent>
#include<QDebug>
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = nullptr);
~MyWidget();
void timerEvent(QTimerEvent *e);
public slots:
private slots:
void on_pushButton_2_clicked();
private:
int id;
Ui::MyWidget *ui;
QString s2;
QTextToSpeech *speecher;
};
#endif // MYWIDGET_H
#include "mywidget.h"
#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
speecher = new QTextToSpeech(this);
//触发timeEvent函数
id=startTimer(100);
}
MyWidget::~MyWidget()
{
delete ui;
}
void MyWidget::timerEvent(QTimerEvent *e)
{ if(e->timerId()==id)
{
QTime sys_time = QTime::currentTime();
QString s=sys_time.toString("hh:mm:ss");
ui->timeLabel->setText(s);
if(s2==s){
speecher->say(ui->label2->text());
}
}
}
void MyWidget::on_pushButton_2_clicked()
{
//输入时间赋值给全局变量s
this->s2=ui->timeEdit->text();
//加粗字体并且设置背景
ui->label2->setStyleSheet("font-weight:bold;background-color:pink");
}