1.思维导图
2.
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QTimerEvent>
#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();
private:
Ui::Widget *ui;
int start_id;
int sys_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);
//启动一个定时器
sys_id = startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
//判断是否是sys_id超时
if(e->timerId() == sys_id)
{
QTime sys = QTime::currentTime();
ui->label->setText(sys.toString("hh:mm:ss"));
}
//判断是否是start_id超时
if(e->timerId() == start_id)
{
if(ui->lineEdit->text() == ui->label->text())
{
for (int i = 0;i<6;i++)
{
speecher->say(ui->label_3->text());
}
}
}
}
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "启动")
{
//启动一个定时器
start_id = startTimer(1000); //每隔1秒钟,自动执行timerEvent()函数
//将文本设置“关闭”
ui->pushButton->setText("关闭");
}
else
{
//关闭定时器
killTimer(start_id);
speecher->stop();
//将文本设置“启动”
ui->pushButton->setText("启动");
}
}