华清远见作业第四十二天——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("启动");
    }

}

运行效果:

相关推荐
≮傷£≯√3 小时前
QGraphicsScene(一) drawCAD 问题
qt
qq_401700411 天前
Qt QUrl 详解与代码示例
开发语言·数据库·qt
雨田言炎1 天前
Qt的常用类QList--序列数据管理
嵌入式硬件·qt
朽棘不雕1 天前
Qt项目代码解释
开发语言·qt
CVer儿1 天前
vs2022配置qt
开发语言·qt
kaixin_learn_qt_ing1 天前
Qt 打开目录多选对话框
qt
lqj_本人2 天前
WinPcap 鸿蒙 PC 适配全记录:从 NPF 原始抓包到 VPN_TUN 与 libpcap 双通路
qt·华为·harmonyos
Quz2 天前
QML StackView:三种入栈方式(Item、Component 与 URL)
qt
Quz2 天前
QML StackView:入栈、出栈、替换与批量操作
qt
老赵的博客2 天前
c++面试之从虚函数表到Rtti一次性讲清楚
c++·qt