【QT】自制一个简单的小闹钟,能够实现语音播报功能

news/2025/2/22 7:55:30/

        做了一个自制的小闹钟,能够自己输入时间,以及对应的闹铃,时间到了自动播放设定的闹铃,可以随时取消重新设定,采用分文件编译

        注意:需要在.pro文件中加入:QT       += core gui texttospeech

代码如下:

wideget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QKeyEvent>     //键盘事件类
#include <QMouseEvent>   //鼠标事件类
#include <QIcon>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QMovie>
#include <QObject>
#include <QMessageBox>
#include <QTimer>            //定时器类
#include <QTime>             //时间类
#include <QTimerEvent>       //定时器事件类
#include <QDateTime>         //日期时间类
#include <QTextToSpeech>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTsignals:public slots:void start_slots();void timeout_slots();     //自定义处理超时信号的槽函数void cancel_slots();     //自定义取消按钮槽函数public:Widget(QWidget *parent = nullptr);~Widget();void mouseMoveEvent(QMouseEvent *event);       //鼠标移动事件void mousePressEvent(QMouseEvent *event);      //鼠标点击事件void speakText(const QString &text);private:Ui::Widget *ui;QPoint temp;                //移动窗口中间辅助向量QLabel *system_time,*lab1;         //显示系统时间QLineEdit *mod_time,*clock_txt;             //可编辑的时间、闹钟输出的文字QPushButton *start_button,*cancel_button;   //启动按钮和取消按钮int tid = 0;       //定时器id号QTimer t1;        //定义一个定时器变量};
#endif // WIDGET_H

main.cpp

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

 widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);this->setFixedSize(500,500);                    //设置页面初始尺寸大小this->setWindowFlag(Qt::FramelessWindowHint);   //将头部隐藏//标签类system_time = new QLabel(this);          //系统时间system_time->resize(200,140);system_time->move(50,50);system_time->setScaledContents(true);system_time->setStyleSheet("background-color:pink; border-radius:20;\font-size:20px;font-weight:bold");system_time->setWindowOpacity(0.1);//按钮类start_button = new QPushButton("启动",this);cancel_button = new QPushButton("取消",this);//设置位置start_button->move(system_time->x()+system_time->width()+15,110);cancel_button->move(start_button->x()+start_button->width()+5,start_button->y());//设置大小start_button->resize(90,80);cancel_button->resize(90,80);// start_button->setStyleSheet("border-radius:20");//行标签类mod_time = new QLineEdit(this);mod_time->move(system_time->y()+system_time->width()+15,system_time->y());mod_time->setPlaceholderText("输入定时");mod_time->resize(180,30);clock_txt = new QLineEdit(this);clock_txt->move(50,230);clock_txt->resize(400,250);clock_txt->setPlaceholderText("输入闹铃");// 设置文本对齐到左上角clock_txt->setAlignment(Qt::AlignLeft | Qt::AlignTop);clock_txt->setStyleSheet("QLineEdit { padding-left: 0px; }");//连接信号与槽,按下按钮开始执行功能connect(start_button, &QPushButton::clicked, this, &Widget::start_slots);connect(&t1, &QTimer::timeout, this, &Widget::timeout_slots);connect(cancel_button, &QPushButton::clicked, this, &Widget::cancel_slots);
}Widget::~Widget()
{delete ui;
}//鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{this->move(event->globalPos() - temp);
}//鼠标点击事件
void Widget::mousePressEvent(QMouseEvent *event)
{temp = event->globalPos() - this->pos();    //求出中间辅助变量if(event->button() == Qt::RightButton){this->close();}
}void Widget::start_slots()
{t1.start(1000);          //每隔指定时间,发送一个timeout信号
}void Widget::timeout_slots()
{//获取系统时间QTime sysTime = QTime::currentTime();//将QTime类对象转变成字符串QString sysTimeStr = sysTime.toString("hh:mm:ss");this->system_time->setText(sysTimeStr);system_time->setAlignment(Qt::AlignCenter);//将3个地方设置成不可点击start_button->setEnabled(false);mod_time->setReadOnly(true);clock_txt->setReadOnly(true);//比较逻辑,如果和我输入的时间相等,就发出声音QString modTimeStr = mod_time->text();if(sysTimeStr == modTimeStr){qDebug()<<"发出声音";speakText(clock_txt->text());}
}void Widget::cancel_slots()
{int res = QMessageBox::information(this,"提示","真的要取消么",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);if(res == QMessageBox::Yes){qDebug()<<"解除限制功能";t1.stop();start_button->setEnabled(true);mod_time->setReadOnly(false);clock_txt->setReadOnly(false);}else if(res == QMessageBox::No){qDebug()<<"继续执行程序";}
}void Widget::speakText(const QString &text)
{QTextToSpeech *speaker = new QTextToSpeech;speaker->say(text);
}

输出结果如下:


http://www.ppmy.cn/news/1524098.html

相关文章

Leetcode 最长连续序列

算法流程&#xff1a; 哈希集合去重&#xff1a; 通过将数组中的所有元素放入 unordered_set&#xff0c;自动去除重复元素。集合的查找操作是 O(1)&#xff0c;这为后续的快速查找提供了保证。 遍历数组&#xff1a; 遍历数组中的每一个元素。对于每个元素&#xff0c;首先检…

深入理解Docker核心原理:全面解析Docker Client

随着云计算与容器技术的飞速发展&#xff0c;Docker已经成为软件开发、部署和运维中的重要工具之一。在Docker的架构中&#xff0c;Docker Client作为用户操作Docker系统的接口&#xff0c;起着至关重要的作用。本文将详细解析Docker Client的核心原理、工作机制、常用命令以及…

复仇时刻 华为的狙击还没结束

文&#xff5c;琥珀食酒社 作者 | 积溪 华为的复仇时刻已到啊 名场面即将再次上演 看过华为和苹果发布会的人 应该都有似曾相识的感觉 去年8月底 雷女士访华第二天 华为发布了Mate 60先锋计划 9月13日苹果发布iPhone 15 恰恰就在这天 华为咔嚓一下 又放出了大折叠屏…

使用Python中的igraph为绘图添加标题和图例

在 igraph 中&#xff0c;可以通过添加标题和图例来增强图形的可读性和表达能力。我们可以使用 igraph.plot 函数进行绘图&#xff0c;并通过它的参数来指定标题和图例。 1、问题背景 在python中的igraph库中&#xff0c;能否为绘图添加图例和标题&#xff1f;在手册或教程中都…

动手学深度学习(pytorch土堆)-02TensorBoard的使用

1.可视化 代码使用了 torch.utils.tensorboard 将数据记录到 TensorBoard 以便可视化。具体来说&#xff0c;它将标量数据记录到目录 logs 中&#xff0c;使用的是 SummaryWriter 类。 代码分解如下&#xff1a; SummaryWriter("logs")&#xff1a;初始化一个 Ten…

Unity 之如何实现基于OpenAI的ChatGPT的聊天机器人

文章目录 前言接入说明Http请求GPT社区库1.C#/.Net的库2.OpenUPM库3.语音对话GPT实现Unity 接入OpenAI1.导入包2.设置你的 OpenAI 帐户3.向 OpenAPI 发出请求4.语音对话功能5.代码实现6.UI界面实现精彩推荐前言 在当前的技术环境中,人工智能聊天机器人越来越普遍。OpenAI的Ch…

搭建Windows下的Rust开发环境

【图书介绍】《Rust编程与项目实战》-CSDN博客 《Rust编程与项目实战》(朱文伟&#xff0c;李建英)【摘要 书评 试读】- 京东图书 (jd.com) Rust编程与项目实战_夏天又到了的博客-CSDN博客 2.1.1 安装vs_buildtools 在Windows系列操作系统中&#xff0c;Rust开发环境需要依…

CSS实现优惠券透明圆形镂空打孔效果等能力学习

前言&#xff1a;无他&#xff0c;仅供学习记录&#xff0c;通过一个简单的优惠券Demo实践巩固CSS知识。 本次案例主要学习或巩固一下几点&#xff1a; 实现一个简单的Modal&#xff1b;如何进行复制文本到粘贴板&#xff1b;在不使用UI的svg图片的情况下&#xff0c;如何用C…