• Qt简易闹钟


    配置文件

    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

    头文件

    widght.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. #include
    14. //#include
    15. QT_BEGIN_NAMESPACE
    16. namespace Ui { class Widget; }
    17. QT_END_NAMESPACE
    18. class Widget : public QWidget
    19. {
    20. Q_OBJECT
    21. public:
    22. Widget(QWidget *parent = nullptr);
    23. ~Widget();
    24. void timerEvent(QTimerEvent *event) override;
    25. private:
    26. Ui::Widget *ui;
    27. //定义一个lable指针, 当前时间
    28. QLabel *current_time_lab;
    29. //定义一个行编辑器指针, 闹钟
    30. QLineEdit *clock_edit;
    31. //定义一个按钮指针, 启动
    32. QPushButton *start_clock_btn;
    33. //定义一个停止闹钟, 关闭
    34. QPushButton *stop_clock_btn;
    35. //定义一个文本编辑器
    36. QTextEdit *text_edit;
    37. //定义一个闹钟语音播放者
    38. QTextToSpeech *clock_speecher;
    39. //定义一个定时器指针
    40. QTimer *clock_timer;
    41. QTimer *clock_timer2;
    42. int time_id;
    43. public slots:
    44. void start_slot();
    45. void cklock_slot();
    46. void stop_slot();
    47. };
    48. #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. int flag = 0;
    4. int clock_flag = 0;
    5. Widget::Widget(QWidget *parent)
    6. : QWidget(parent)
    7. , ui(new Ui::Widget)
    8. {
    9. ui->setupUi(this);
    10. //组件实例化
    11. current_time_lab = new QLabel(this);
    12. clock_edit = new QLineEdit(this);
    13. start_clock_btn = new QPushButton("启动", this);
    14. stop_clock_btn = new QPushButton("停止", this);
    15. text_edit = new QTextEdit(this);
    16. clock_speecher = new QTextToSpeech();
    17. clock_timer = new QTimer(this);
    18. clock_timer2 = new QTimer(this);
    19. //设置页面布局
    20. //固定窗口大小
    21. this->setFixedSize(500,400);
    22. //this->setStyleSheet("background-img");
    23. current_time_lab->resize(300, 70);
    24. current_time_lab->move(40,30);
    25. current_time_lab->setStyleSheet("background-color:rgba(137, 199, 117, 0.7);font:幼圆; font-size:26px;");
    26. current_time_lab->setAlignment(Qt::AlignCenter);
    27. clock_edit->resize(100, 30);
    28. //clock_edit->setPlaceholderText("定时时间");
    29. clock_edit->move(this->width()-140, 30);
    30. clock_edit->setAlignment(Qt::AlignHCenter);
    31. start_clock_btn->resize(45, 30);
    32. start_clock_btn->move(clock_edit->x(), clock_edit->y()+40);
    33. start_clock_btn->setStyleSheet("font:幼圆; font-size:10px; padding:2px");
    34. stop_clock_btn->resize(start_clock_btn->size());
    35. stop_clock_btn->move(start_clock_btn->x()+55, start_clock_btn->y());
    36. stop_clock_btn->setStyleSheet("font:幼圆; font-size:10px; padding:2px");
    37. text_edit->resize(this->width()-80, 220);
    38. text_edit->move(current_time_lab->x(), current_time_lab->y()+current_time_lab->height()+40);
    39. stop_clock_btn->setDisabled(true);
    40. clock_timer->start(1000);
    41. connect(clock_timer, &QTimer::timeout, this, [&](){
    42. QDateTime date_time = QDateTime::currentDateTime();
    43. current_time_lab->setText(date_time.toString("yyyy-MM-dd hh:mm:ss"));
    44. QTime currenttime = QTime::currentTime();
    45. QString time = currenttime.toString("hh:mm:ss");
    46. if(time <= clock_edit->text())
    47. {
    48. clock_flag = 0;
    49. }
    50. });
    51. connect(clock_timer, &QTimer::timeout, this, &Widget::cklock_slot);
    52. connect(start_clock_btn, &QPushButton::clicked, this, &Widget::start_slot);
    53. connect(stop_clock_btn, &QPushButton::clicked, this, &Widget::stop_slot);
    54. //time_id = startTimer(0);
    55. }
    56. Widget::~Widget()
    57. {
    58. delete ui;
    59. }
    60. void Widget::start_slot()
    61. {
    62. start_clock_btn->setEnabled(false);
    63. stop_clock_btn->setEnabled(true);
    64. clock_edit->setDisabled(true);
    65. }
    66. void Widget::cklock_slot()
    67. {
    68. if(!start_clock_btn->isEnabled())
    69. {
    70. QTime currenttime = QTime::currentTime();
    71. QString time = currenttime.toString("hh:mm:ss");
    72. if(time >= clock_edit->text()&& clock_flag == 0)
    73. {
    74. if(flag == 0)
    75. {
    76. clock_speecher->say(text_edit->toPlainText());
    77. clock_timer2->start(2000);
    78. connect(clock_timer2, &QTimer::timeout, [](){
    79. flag = 0;});
    80. }
    81. flag =1;
    82. }
    83. }
    84. else
    85. clock_speecher->stop();
    86. }
    87. void Widget::stop_slot()
    88. {
    89. //clock_speecher->stop();
    90. start_clock_btn->setEnabled(true);
    91. stop_clock_btn->setEnabled(false);
    92. clock_edit->setEnabled(true);
    93. clock_flag = 1;
    94. }
    95. //定时器事件处理函数的实现
    96. void Widget::timerEvent(QTimerEvent *event)
    97. {
    98. if(event->timerId() == time_id)
    99. {
    100. //static int num = 0;
    101. //ui->eventtime->setNum(++num); //setNum()函数是设置文本内容数字
    102. QDateTime sys_dt = QDateTime::currentDateTime(); //获取当前系统的日期时间
    103. this->current_time_lab->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss"));
    104. this->current_time_lab->setAlignment(Qt::AlignCenter);
    105. if(!start_clock_btn->isEnabled())
    106. {
    107. QTime currenttime = QTime::currentTime();
    108. QString time = currenttime.toString("hh:mm:ss");
    109. if(time == clock_edit->text())
    110. {
    111. // if(flag == 0)
    112. // {
    113. clock_speecher->say(text_edit->toPlainText());
    114. // clock_timer2->start(2000);
    115. // connect(clock_timer2, &QTimer::timeout, [](){
    116. // flag = 0;});
    117. //}
    118. //flag =1;
    119. }
    120. }
    121. else
    122. clock_speecher->stop();
    123. }
    124. }

    Qt简易闹钟

  • 相关阅读:
    Yolov5(v5.0) + pyqt5界面设计
    校园报修抢修小程序系统开发 物业小区报修预约上门维修工单系统
    我开源了一个加密算法仓库,支持18种算法!登录注册业务可用!
    CocosCreator3.8研究笔记(十四)CocosCreator 资源管理Asset Manager
    【开题报告】基于SpringBoot的美术馆预约平台的设计与实现
    如何用webgl(three.js)搭建一个3D库房,3D仓库3D码头,3D集装箱,车辆定位,叉车定位可视化孪生系统——第十五课
    MyBatis查询数据库
    linux环境下的MySQL UDF提权
    12、建立健全人员培训体系
    springcloudAlibaba之Nacos服务注册源码分析
  • 原文地址:https://blog.csdn.net/qq_46766479/article/details/132657487