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);
}

效果图:

相关推荐
端平入洛1 天前
delete又未完全delete
c++
端平入洛2 天前
auto有时不auto
c++
哇哈哈20213 天前
信号量和信号
linux·c++
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马3 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝3 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc3 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼3 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx3 天前
DHU上机打卡D31
开发语言·c++·算法
czxyvX3 天前
020-C++之unordered容器
数据结构·c++