55、Qt/事件机制相关学习20240326

一、代码实现设置闹钟,到时间后语音提醒用户。示意图如下:

代码:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , speecher(new QTextToSpeech(this))
{
    ui->setupUi(this);
    //启动一个定时器 每隔一秒执行一次timerEvent函数
    id = startTimer(1000);
}

Widget::~Widget()
{
    delete ui;
}
//定时器事件函数实现
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == id)
    {
        //获取系统时间 Qtime类
        QTime sys_time = QTime::currentTime();
        //将时间转换成字符串 写入显示时间的Label中
        ui->systime_label->setText(sys_time.toString("hh:mm:ss"));
        //居中显示
        ui->systime_label->setAlignment(Qt::AlignCenter);

    }else if(e->timerId() == id2)
    {
        if(ui->systime_label->text() == ui->setclock_Edit->text())
        {
            for(int i = 0;i < 3;i++)
            {
                speecher->say(ui->speak_label->text());
            }
        }
    }
}


void Widget::on_start_Btn_clicked()
{
    //点击启动按钮  设置闹钟
    id2 = startTimer(1000);
}

运行:

思维导图:

相关推荐

  1. Qt事件机制

    2024-03-27 05:44:05       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-27 05:44:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-27 05:44:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-27 05:44:05       82 阅读
  4. Python语言-面向对象

    2024-03-27 05:44:05       91 阅读

热门阅读

  1. 量子计算与大模型融合的潜力与挑战探索

    2024-03-27 05:44:05       38 阅读
  2. 简明 Python 教程(第5章 函数)

    2024-03-27 05:44:05       32 阅读
  3. Python 和 Go:一文了解

    2024-03-27 05:44:05       38 阅读
  4. 151 shell编程

    2024-03-27 05:44:05       30 阅读
  5. React中的受控组件与非受控组件

    2024-03-27 05:44:05       35 阅读
  6. go | switch/case、array、slices、range、functions

    2024-03-27 05:44:05       41 阅读
  7. DMA知识

    DMA知识

    2024-03-27 05:44:05      39 阅读