思维导图:
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QTimerEvent>
#include <QPushButton>
#include <QTextToSpeech>
#include <QDebug>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *e);
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
QTextToSpeech *speech;
int id;
int secid;
};
#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);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setWindowFlag(Qt::FramelessWindowHint);
id = startTimer(1000);//启动定时器
speech = new QTextToSpeech(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == id)
{
QTime sys_time = QTime::currentTime();
ui->syslabel->setText(sys_time.toString("hh:mm:ss"));
}else if(e->timerId() == secid)
{
if(ui->syslabel->text() == ui->lineEdit->text())
{
//qDebug() << ui->textlabel->text();
int i = 0;
while(i<10)
{
speech->say(ui->textlabel->text());
i++;
}
killTimer(secid);
}
}
}
void Widget::on_pushButton_clicked()
{
secid = startTimer(1000);
}
?效果图: