• QT设置闹钟超时播报


    头文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. QT_BEGIN_NAMESPACE
    8. namespace Ui { class Widget; }
    9. QT_END_NAMESPACE
    10. class Widget : public QWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. Widget(QWidget *parent = nullptr);
    15. ~Widget();
    16. void timerEvent(QTimerEvent *e);
    17. private slots:
    18. void on_pushButton_clicked();
    19. private:
    20. Ui::Widget *ui;
    21. int id;
    22. int id1;
    23. QTextToSpeech *speecher;
    24. };
    25. #endif // WIDGET_H

    测试文件

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent) // 调用基类的构造函数,设置父对象
    5. , ui(new Ui::Widget) // 实例化UI对象
    6. , speecher(new QTextToSpeech(this)) // 实例化QTextToSpeech对象,并设置当前Widget为父对象
    7. {
    8. ui->setupUi(this); /
    9. id = startTimer(1000); // 启动一个定时器,每隔1000毫秒(1秒)触发一次timerEvent
    10. // 设置lineEdit的样式
    11. ui->lineEdit->setStyleSheet("background-color: rgba(255, 255, 255, 150); border: 1px solid gray; border-radius: 5px;");
    12. }
    13. Widget::~Widget()
    14. {
    15. delete ui;
    16. }
    17. // 当pushButton被点击时调用的槽函数
    18. void Widget::on_pushButton_clicked()
    19. {
    20. id1 = startTimer(1000); // 启动另一个定时器,与id不同,用于不同的定时任务
    21. }
    22. // 重写QWidget的timerEvent函数,处理定时器事件
    23. void Widget::timerEvent(QTimerEvent *e)
    24. {
    25. // 检查是哪个定时器触发了事件
    26. if(e->timerId()==id)
    27. {
    28. QTime sys_time=QTime::currentTime(); // 获取当前系统时间
    29. QString t=sys_time.toString("hh--mm--ss"); // 将时间格式化为字符串
    30. ui->label->setText(t); // 将时间字符串设置到label上
    31. }
    32. else if(e->timerId() == id1)
    33. {
    34. // 检查lineEdit中的文本是否与label中的文本相同
    35. if(ui->lineEdit->text() == ui->label->text())
    36. {
    37. // 如果相同,则使用QTextToSpeech播放label_3中的文本5
    38. for(int i=0; i<5;i++)
    39. {
    40. speecher->say(ui->label_3->text());
    41. }
    42. }
    43. }
    44. }

  • 相关阅读:
    【HBZ分享】TCP可靠性传输如何保证的?以及传输优化之Nagle算法
    【MySQL】数据库基础
    《工程伦理与学术道德》第二章习题
    C/C++中 extern用法
    三网优惠话费充值接口源码文档 支持批量下单
    STC8H开发(十四): I2C驱动RX8025T高精度实时时钟芯片
    Allegro 172版本自动放置层叠
    当大火的文图生成模型遇见知识图谱,AI画像趋近于真实世界
    Day13:vw 和 vh 基本使用
    websocket加鉴权 @ServerEndpoint方式
  • 原文地址:https://blog.csdn.net/m0_75109289/article/details/142147250