1.思维导图
2.
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QPushButton>
#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 id;
int id2;
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);
id = startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
//获取系统时间
QTime sys_time = QTime::currentTime();
//将时间转换成字符串
QString t = sys_time.toString("hh:mm:ss");
//判断是哪个定时器超时
if(e->timerId() == id)
{
//在lab上显示实时时间
ui->label_3->setText(t);
ui->label_3->setAlignment(Qt::AlignCenter);
}
if(e->timerId() == id2)
{
//当定时器时间与设定时间相同时
if(ui->lineEdit->text() == t)
{
//循环5次播放text
for(int i=1;i<=5;i++){
ui->label_2->setText("好好学习,天天向上,懒虫睡觉");
speecher->say(ui->label_2->text());
}
//关闭定时器
killTimer(id2);
}
}
}
//点击按钮启动定时器2
void Widget::on_pushButton_clicked()
{
id2 = startTimer(1000);
}