QTday5

发布于:2025-02-11 ⋅ 阅读:(32) ⋅ 点赞:(0)

Label上显示系统时间,程序运行就要有。
Lable2,:请输入闹钟的事件 行编辑器和启动按钮。启动按钮之后,到了行编辑器内的时间后,就开始语音播报最下方label中的内容。
语音播报5次
在这里插入图片描述

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>
#include <QTime>           // 时间类
#include <QTimerEvent>     // 定时事件类
#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) override;

private slots:
    void on_timerPB_clicked();    // 启动定时器的槽函数

private:
    Ui::Widget *ui;
    int id;           // 定时器1的ID(系统时间)
    int id2;          // 定时器2的ID(闹钟定时器)
    QTextToSpeech *speecher; // 语音播报器对象
};

#endif // WIDGET_H

 widget.cpp

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , speecher(new QTextToSpeech(this)) // 初始化语音播报器
{
    ui->setupUi(this);

    // 显示当前系统时间到 systimeLab
    QString t = QTime::currentTime().toString("hh:mm:ss"); // 获取当前系统时间并格式化为字符串
    ui->systimeLab->setText("系统时间:" + t); // 将当前时间显示在 systimeLab 标签上
    ui->systimeLab->setAlignment(Qt::AlignCenter); // 设置标签文本居中显示

    // 启动一个 1 秒间隔的定时器,用于实时更新系统时间
    id = startTimer(1000); // 定时器以 1 秒为间隔触发
}

Widget::~Widget()
{
    delete ui; // 释放 UI 资源
}

// 定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e)
{
    // 判断是否为系统时间更新的定时器
    if (e->timerId() == id)
    {
        // 实时更新系统时间
        QTime sys_time = QTime::currentTime();       // 获取当前时间
        QString t = sys_time.toString("hh:mm:ss");   // 转换为 "hh:mm:ss" 格式的字符串
        ui->systimeLab->setText(t);                 // 更新 systimeLab 标签显示时间
    }
    // 判断是否为闹钟触发的定时器
    else if (e->timerId() == id2)
    {
        // 获取用户输入的文本内容
        QString textToSpeak = ui->txtLab->text(); // 从 txtLab 文本框中获取需要播报的文本内容

        // 循环播报文本三遍
        for (int i = 0; i < 3; ++i)
        {
            speecher->say(textToSpeak);           // 使用 QTextToSpeech 播报文本内容
            qDebug() << "语音播报第" << (i + 1) << "次:" << textToSpeak; // 在调试输出中记录播报次数和内容
        }

        // 关闭闹钟定时器
        killTimer(id2); // 停止闹钟定时器
        id2 = 0;        // 将闹钟定时器 ID 重置为 0

        // 在事件标签中显示提示信息
        ui->eventLab->setText("闹钟触发,已播报三遍!"); // 在 eventLab 标签中显示提示信息
        ui->eventLab->setAlignment(Qt::AlignCenter);      // 设置标签文本居中显示
    }
}

// 启动定时器按钮的槽函数
void Widget::on_timerPB_clicked()
{
    // 获取用户设置的目标时间
    QString t1 = ui->lineEdit->text(); // 从 lineEdit 输入框中读取用户输入的时间
    QTime time1 = QTime::currentTime(); // 获取当前时间
    QTime time2 = QTime::fromString(t1, "hh:mm:ss"); // 将用户输入的时间转换为 QTime 对象

    // 检查用户输入的时间是否有效
    if (!time2.isValid() || time1 >= time2) // 检查时间格式是否正确,且目标时间晚于当前时间
    {
        qDebug() << "输入时间无效或早于当前时间"; // 输出调试信息
        ui->eventLab->setText("时间无效,请输入有效时间!"); // 在 eventLab 标签中提示用户输入错误
        ui->eventLab->setAlignment(Qt::AlignCenter);       // 设置标签文本居中显示
        return; // 退出函数,不设置定时器
    }

    // 计算当前时间与目标时间的时间差(以毫秒为单位)
    int cha = time1.msecsTo(time2); // 计算目标时间与当前时间之间的毫秒数差
    qDebug() << "相差毫秒数:" << cha; // 输出调试信息,显示时间差

    // 启动闹钟定时器
    id2 = startTimer(cha); // 设置定时器,超时时间为目标时间与当前时间的差值
    ui->eventLab->setText("闹钟已设置!"); // 在 eventLab 标签中提示闹钟已设置
    ui->eventLab->setAlignment(Qt::AlignCenter); // 设置标签文本居中显示
    qDebug() << "闹钟设置成功,间隔时间:" << cha / 1000 << "秒"; // 输出调试信息,显示闹钟触发间隔
}


网站公告

今日签到

点亮在社区的每一天
去签到