• QT day4


    在这里插入图片描述

    // widget.h
    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include 
    #include  //时间类
    #include  // 定时器事件类
    #include  // 定时器类
    #include  //
    
    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); // 定时器事件函数重写
    
    signals:
        void my_signal(); // 自定义信号函数
    
    private slots:
        void on_startBtn_clicked(); // 启动按钮对应的槽函数
    
    private:
        Ui::Widget *ui;
    
        int tId; // 定时器的Id
    
        QTextToSpeech* speecher; // 用QTextToSpeech实例化一个speecher对象
    };
    #endif // WIDGET_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    // widget.cpp
    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
        tId = startTimer(1000); // 启动一个定时器, 延时1000毫秒
    
        ui->tipEdit->setAlignment(Qt::AlignCenter); // 文字居中显示
    
        speecher = new QTextToSpeech(this); // 给speecher初始化和申请空间
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::timerEvent(QTimerEvent *e)
    {
        if (e->timerId() == tId) // 判断是哪个定时器
        {
            QTime time = QTime::currentTime(); // 获取当前系统时间
            QString displayStr = time.toString("hh:mm:ss"); // 将time的类型转换为QString类型
            // 判断系统时间是否与输入的时间相等
            if (displayStr == ui->timeLab->text())
            {
                int num = 5;
                while (num--)
                {
                    speecher->say(ui->label->text()); // 把label中的文字转换成语音
                }
            }
            ui->displayLab->setText(displayStr);
            ui->displayLab->setAlignment(Qt::AlignCenter);
        }
    }
    
    
    void Widget::on_startBtn_clicked()
    {
        ui->timeLab->setText(ui->tipEdit->text());
        ui->timeLab->setAlignment(Qt::AlignCenter);
        ui->tipEdit->clear(); // 清空输入框
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    思维导图
    在这里插入图片描述

  • 相关阅读:
    Win11一些问题以及解决方案
    SpringBoot+Vue实现前后端分离灾情救援系统
    2022世界人工智能大会 “智慧金融与数字员工”分论坛在沪成功举办
    Flutter的专属Skia引擎解析+用法原理
    使用Flask和Flask-JWT-Extended保护API免受跨站请求攻击
    【Linux】Linux环境搭建
    02.机器学习原理(复习)
    基于ssm高校档案管理系统源码
    Java基础
    Github每日精选(第9期):bcc跟踪内核和操作程序的工具集
  • 原文地址:https://blog.csdn.net/m0_51235177/article/details/134441770