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

}

运行效果:

相关推荐
xcyxiner3 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner3 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner4 天前
DicomViewer (添加模型类)3
qt
xcyxiner4 天前
DicomViewer (目录调整) 2
qt
xcyxiner5 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能6 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G7 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
森G7 天前
77、线程池原理和实现------服务器源码解析----云视频服务项目
服务器·c++·qt
森G7 天前
71、打包发布---------打包发布
c++·qt
初圣魔门首席弟子7 天前
Node.js 详细介绍(知识库版)
windows·qt·node.js·知识库