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;
}
相关推荐
希望20172 小时前
Golang Panic & Throw & Map/Channel 并发笔记
开发语言·golang
朗迹 - 张伟2 小时前
Golang安装笔记
开发语言·笔记·golang
yzx9910132 小时前
生活在数字世界:一份人人都能看懂的网络安全生存指南
运维·开发语言·网络·人工智能·自动化
小周同学@2 小时前
谈谈对this的理解
开发语言·前端·javascript
橙*^O^*安3 小时前
Go 语言基础:变量与常量
运维·开发语言·后端·golang·kubernetes
NiKo_W3 小时前
Linux 文件系统与基础指令
linux·开发语言·指令
工程师小星星3 小时前
Golang语言的文件组织方式
开发语言·后端·golang
乂爻yiyao3 小时前
java 代理模式实现
java·开发语言·代理模式
张子夜 iiii4 小时前
实战项目-----Python+OpenCV 实现对视频的椒盐噪声注入与实时平滑还原”
开发语言·python·opencv·计算机视觉
2301_770373734 小时前
Java集合
java·开发语言