QT使用定时器事件驱动完成定时播报效果

cpp 复制代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QObject>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QTimer>
#include <QString>
#include <QTime>
#include <QTextEdit>
#include <QMessageBox>
#include <QDebug>
#include <QtTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    static QTime sysTime;
    QPushButton *StartUp;
    QPushButton *Cancel;
    QLineEdit *TimeEdit;
    QTextEdit *PoetryEdit;
    QLabel *SysTime;
    QTextToSpeech *speech;

    void timerEvent(QTimerEvent *event) override;
    int timerId;

public slots:
    // 显示系统时间
    void SysTimeShow();
    // 启动定时器
    void StartTimer();
    // 关闭定时器

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
cpp 复制代码
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QTextToSpeech>

QTime Widget::sysTime;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
   , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(500, 500);

    // 启动按钮
    StartUp = new QPushButton(this);
    StartUp->setText("启动");
    StartUp->setFixedSize(70, 40);
    StartUp->move(270, 150);
    connect(StartUp, &QPushButton::clicked, this, &Widget::StartTimer);

    // 取消按钮
    Cancel = new QPushButton(this);
    Cancel->setText("取消");
    Cancel->setFixedSize(70, 40);
    Cancel->move(370, 150);

    // 时间行编辑器
    TimeEdit = new QLineEdit(this);
    TimeEdit->setFixedSize(180, 40);
    TimeEdit->move(270, 100);

    // 诗句行编辑器
    PoetryEdit = new QTextEdit(this);
    PoetryEdit->setFixedSize(400, 250);
    PoetryEdit->move(50, 200);

    // 系统时间标签
    SysTime = new QLabel(this);
    SysTime->setFixedSize(190, 85);
    SysTime->move(50, 100);
    SysTime->setStyleSheet("background-color:rgb(248,227,228)");
    SysTime->setAlignment(Qt::AlignCenter);

    // 播报
    speech = new QTextToSpeech(this);

    timerId = startTimer(1000);
}

// 显示系统时间
void Widget::SysTimeShow()
{
    Widget::sysTime = QTime::currentTime();
    QString tm = Widget::sysTime.toString("hh::mm::ss");
    SysTime->setText(tm);
    qDebug() << tm;
}

// 启动按钮
void Widget::StartTimer()
{
    // 获取输入的时间
    QString TimeStr = TimeEdit->text();

    QString Sys = sysTime.toString("hh::mm::ss");

    qDebug() << TimeStr;
    qDebug() << Sys;
}

void Widget::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == timerId)
    {
        SysTimeShow();
        QString TimeStr = TimeEdit->text();
        QString Sys = sysTime.toString("hh::mm::ss");
        if (TimeStr == Sys)
        {
            // 播报诗句行编辑器中的内容
            speech->say(PoetryEdit->toPlainText());
        }
    }
}

Widget::~Widget()
{
    delete ui;
    delete speech;
}
相关推荐
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔3 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号3 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_3 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty3 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再3 天前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame