思维导图:
编程:
代码:
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QTextToSpeech> //语音播报类
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *e); //重写定时器事件函数的声明private slots:void on_pushButton_clicked();private:Ui::Widget *ui;int id;//超时1int id2;//超时2QString t;//实例化一个语音播放类QTextToSpeech *speecher;QString s;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<QTime>
#include<QDebug>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{speecher=new QTextToSpeech(this); //语音ui->setupUi(this);id=startTimer(1000);//超时器1}Widget::~Widget()
{delete ui;
}
//当定时器超时时,自动执行的函数
void Widget::timerEvent(QTimerEvent *e)
{//判断那个定时器超时if(e->timerId()==id){
// static int num=0;
// ui->lab1->setNum(++num);//获取系统时间QTime sys_time =QTime::currentTime();//将系统时间转换为字符串类型t=sys_time.toString("hh:mm:ss");//将内容显示到ui界面上ui->label->setText(t);//居中ui->label->setAlignment(Qt::AlignCenter);}else if(e->timerId()==id2){if(s==t&& ui->pushButton ->text()=="关闭")for (int i=0;i<5;i++) {qDebug() << "成功";speecher->say(ui->label_3->text());//读取内容}}
}void Widget::on_pushButton_clicked()
{if(ui->pushButton ->text() == "启动"){//将按钮上的文本设置成“关闭”s=ui->lineEdit->text();id2=startTimer(1000);//超时2ui->pushButton ->setText("关闭");}else{//将按钮上的文本设置成“启动”ui->pushButton ->setText("启动");}}
运行效果: