华清远见作业第四十二天——Qt(第四天)

思维导图:

编程:

代码:

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#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;//超时1
    int id2;//超时2
    QString t;
    //实例化一个语音播放类
    QTextToSpeech *speecher;
    QString s;
};
#endif // WIDGET_H

widget.cpp

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);//超时2
        ui->pushButton ->setText("关闭");
    }
    else
    {
        //将按钮上的文本设置成"启动"
        ui->pushButton ->setText("启动");
    }

}

运行效果:

相关推荐
扶尔魔ocy8 小时前
【qt openGL入门笔记】FPS相机
qt·opengl
扶尔魔ocy10 小时前
qt openGL入门笔记】FPS相机和轨道相机
qt·opengl
尘客-追梦11 小时前
qmake / jom / 影子构建:把编译管起来
开发语言·c++·qt
qeen8711 小时前
【QT】 信号与槽初始
开发语言·笔记·qt·学习
Quz2 天前
QML PathView:吸附对齐与环形轮播
qt
时间的拾荒人2 天前
Qt 多线程详解:从 QThread 到实战
开发语言·qt·面试
时间的拾荒人2 天前
Qt 事件机制全解析:从事件处理到事件过滤器
开发语言·qt·面试
时间的拾荒人2 天前
Qt 窗口系统详解:从菜单栏到对话框的完整指南
开发语言·qt·面试
qq_401700412 天前
Qt 炫酷曲线,图表,2D/3D开源库
开发语言·qt
扶尔魔ocy2 天前
【qt openGL入门笔记】基础光照
qt·opengl