• 用Qt自制一个小闹钟


    小闹钟 

    功能

    当按下启动按钮时,停止按钮可用,启动按钮不可用,闹钟无法设置,无法输入自定义内容

    当按下停止按钮时,暂停播报,启动按钮可用,闹钟可以设置,可以输入自定义内容

    .pro文件

    1. QT += core gui texttospeech
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. CONFIG += c++11
    4. # The following define makes your compiler emit warnings if you use
    5. # any Qt feature that has been marked deprecated (the exact warnings
    6. # depend on your compiler). Please consult the documentation of the
    7. # deprecated API in order to know how to port your code away from it.
    8. DEFINES += QT_DEPRECATED_WARNINGS
    9. # You can also make your code fail to compile if it uses deprecated APIs.
    10. # In order to do so, uncomment the following line.
    11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    13. SOURCES += \
    14. main.cpp \
    15. widget.cpp
    16. HEADERS += \
    17. widget.h
    18. FORMS += \
    19. widget.ui
    20. # Default rules for deployment.
    21. qnx: target.path = /tmp/$${TARGET}/bin
    22. else: unix:!android: target.path = /opt/$${TARGET}/bin
    23. !isEmpty(target.path): INSTALLS += target
    24. RESOURCES += \
    25. Icon.qrc

    widget.h文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include //定时器类
    5. #include //时间类
    6. #include //定时器事件类的头文件
    7. #include //日期时间类
    8. #include
    9. #include
    10. #include //朗读
    11. #include
    12. #include
    13. QT_BEGIN_NAMESPACE
    14. namespace Ui { class Widget; }
    15. QT_END_NAMESPACE
    16. class Widget : public QWidget
    17. {
    18. Q_OBJECT
    19. public:
    20. Widget(QWidget *parent = nullptr);
    21. ~Widget();
    22. //重写定时器事件处理函数
    23. void timerEvent(QTimerEvent *event)override;
    24. signals:
    25. void my_signal();
    26. private slots:
    27. void on_pushButton_clicked();
    28. void on_pushButton_2_clicked();
    29. private:
    30. Ui::Widget *ui;
    31. //定义一个定时器的id
    32. int timer_id; //基于事件处理函数的定时器
    33. int timer_id1;
    34. QTextToSpeech *speech;
    35. int i = 0;
    36. int flag = 0;
    37. QString text;
    38. QDateTime sys_dt;
    39. };
    40. #endif // WIDGET_H

    main.cpp文件

    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    widget.cpp文件

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. this->setWindowTitle("小闹钟");
    9. timer_id = this->startTimer(5);
    10. ui->pushButton_2->setEnabled(false);
    11. ui->textEdit->setPlaceholderText("请输入闹钟响时播报的内容");
    12. this->setWindowIcon(QIcon(":/new/prefix1/666.png"));
    13. }
    14. Widget::~Widget()
    15. {
    16. delete ui;
    17. }
    18. void Widget::timerEvent(QTimerEvent *event)
    19. {
    20. if(event->timerId()) //== timer_id) //用来判断不同的定时器的id
    21. {
    22. //获取当前系统的日期时间
    23. sys_dt = QDateTime::currentDateTime();
    24. //展示时间到ui界面的lable2中
    25. ui->label->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss"));
    26. //居中显示 标签文本对齐方式
    27. ui->label->setAlignment(Qt::AlignCenter);
    28. ui->label->setFont(QFont("微软雅黑",20));
    29. QString timeText = sys_dt.toString("yyyy-MM-dd hh:mm:ss");
    30. QString timeText1 = ui->dateTimeEdit->text();
    31. if(flag == 1)
    32. {
    33. if(timeText1 == timeText)
    34. {
    35. speech->say(text); // 朗读文本
    36. }
    37. }
    38. }
    39. }
    40. void Widget::on_pushButton_clicked()
    41. {
    42. flag = 1;
    43. speech = new QTextToSpeech;
    44. text = ui->textEdit->toPlainText();
    45. ui->pushButton_2->setEnabled(true);
    46. ui->pushButton->setEnabled(false);
    47. ui->textEdit->setEnabled(false);
    48. ui->dateTimeEdit->setEnabled(false);
    49. }
    50. void Widget::on_pushButton_2_clicked()
    51. {
    52. flag = 0;
    53. ui->pushButton->setEnabled(true);
    54. ui->pushButton_2->setEnabled(false);
    55. ui->textEdit->setEnabled(true);
    56. ui->dateTimeEdit->setEnabled(true);
    57. speech->stop();
    58. }

    widget.ui文件

     

  • 相关阅读:
    电商数据API接口:新服务下电商网站、跨境电商独立站,移动APP的新型拉新武器
    十二张图:从0开始理解对称/非对称加密、CA认证、以及K8S各组件颁发证书原由
    Git——解决 TortoiseGit 提示 No supported authentication methods available 错误
    dfs思路怎样都学不明白是什么原因
    目标检测1——YOLO数据标注以及xml转为txt文件脚本实战
    C++ 常见面试题精选
    [附源码]java毕业设计小区物业管理系统
    【数据结构】八大经典排序(两万字大总结)
    SpringMVC学习---第二课
    SpringBoot+Vue项目医疗管理系统
  • 原文地址:https://blog.csdn.net/ck0056/article/details/132657347