头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QtTextToSpeech>
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();
private:
Ui::Widget *ui;
int id;
QTextToSpeech *speecher;
};
#endif // WIDGET_H
2源文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->label->setAlignment(Qt::AlignCenter);
ui->lineEdit->setAlignment(Qt::AlignCenter);
id=startTimer(1000);
speecher = new QTextToSpeech(this);
this->setWindowFlag(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId()==id)
{
QTime sys_time = QTime::currentTime();
QString s = sys_time.toString("hh:mm:ss");
ui->label->setText(s);
}
else
{
if(ui->lineEdit->text()==ui->label->text())
{
int i=5;
while(i--)
{
speecher->say(ui->label_3->text());
}
}
}
}
void Widget::on_pushButton_clicked()
{
startTimer(1000);
}