Qt12.12

头文件:

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTime>
#include <QTimer>
#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);
private slots:
    void on_btn1_clicked();

    void on_btn2_clicked();

private:
    Ui::Widget *ui;
    int id;
    QTextToSpeech *speecher;
};
#endif // WIDGET_H

源文件:

复制代码
#include "widget.h"
#include "ui_widget.h"

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

    //给语言播报者实例化空间
    speecher = new QTextToSpeech(this);
    // 设置窗口标题
    this->setWindowTitle("闹钟");

     id = startTimer(1000);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == id)
    {
        QTime systime = QTime::currentTime();
        QString s = systime.toString("hh:mm:ss");
        ui->syslab->setText(s);
        ui->syslab->setAlignment(Qt::AlignCenter);

        if(ui->lineEdit->text() == s)
        {
            for(int i=0;i<5;i++)
            {
                speecher->say(ui->mvlab->text());
            }
        }
    }
}

void Widget::on_btn1_clicked()
{
    id = startTimer(1000);
}

void Widget::on_btn2_clicked()
{
    killTimer(id);
}

效果图:

相关推荐
clint45620 小时前
C++进阶(1)——前景提要
c++
夜悊1 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴1 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0012 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾2 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you2 天前
constexpr函数
c++
凡人叶枫2 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫2 天前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
小胖xiaopangss2 天前
BRpc使用
c++·rpc
-森屿安年-2 天前
63. 不同路径 II
c++·算法·动态规划