QT设置闹钟超时播报

头文件

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QTimerEvent>
#include<QTime>
#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_pushButton_clicked();

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

测试文件

复制代码
#include "widget.h"    
#include "ui_widget.h"  
   
Widget::Widget(QWidget *parent)  
    : QWidget(parent) // 调用基类的构造函数,设置父对象  
    , ui(new Ui::Widget) // 实例化UI对象  
    , speecher(new QTextToSpeech(this)) // 实例化QTextToSpeech对象,并设置当前Widget为父对象  
{  
    ui->setupUi(this); / 
    id = startTimer(1000); // 启动一个定时器,每隔1000毫秒(1秒)触发一次timerEvent  
  
    // 设置lineEdit的样式  
    ui->lineEdit->setStyleSheet("background-color: rgba(255, 255, 255, 150); border: 1px solid gray; border-radius: 5px;");  
}  
    
Widget::~Widget()  
{  
    delete ui;   
}  
  
// 当pushButton被点击时调用的槽函数  
void Widget::on_pushButton_clicked()  
{  
    id1 = startTimer(1000); // 启动另一个定时器,与id不同,用于不同的定时任务  
}  
  
// 重写QWidget的timerEvent函数,处理定时器事件  
void Widget::timerEvent(QTimerEvent *e)  
{  
    // 检查是哪个定时器触发了事件  
    if(e->timerId()==id)  
    {  
        QTime sys_time=QTime::currentTime(); // 获取当前系统时间  
        QString t=sys_time.toString("hh--mm--ss"); // 将时间格式化为字符串  
        ui->label->setText(t); // 将时间字符串设置到label上  
    }  
    else if(e->timerId() == id1)  
    {  
        // 检查lineEdit中的文本是否与label中的文本相同  
        if(ui->lineEdit->text() == ui->label->text())  
        {  
            // 如果相同,则使用QTextToSpeech播放label_3中的文本5次  
            for(int i=0; i<5;i++)  
            {  
                speecher->say(ui->label_3->text());  
            }  
        }  
    }  
}
相关推荐
blasit2 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
郑州光合科技余经理7 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1237 天前
matlab画图工具
开发语言·matlab
dustcell.7 天前
haproxy七层代理
java·开发语言·前端
norlan_jame7 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone7 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054967 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月7 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237177 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian7 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript