• QT day4


    1> 实现闹钟功能

    头文件

    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. QT_BEGIN_NAMESPACE
    13. namespace Ui { class Widget; }
    14. QT_END_NAMESPACE
    15. class Widget : public QWidget
    16. {
    17. Q_OBJECT
    18. public:
    19. Widget(QWidget *parent = nullptr);
    20. ~Widget();
    21. void timerEvent(QTimerEvent *event)override;
    22. private slots:
    23. void fixed_time_clicked();
    24. void cancel_clicked();
    25. void mousePressEvent(QMouseEvent *event) override;
    26. void mouseMoveEvent(QMouseEvent *event) override;
    27. private:
    28. Ui::Widget *ui;
    29. QLabel *back; //背景
    30. QLabel *TIME; //实时显示时间
    31. QPushButton *fixed_time; //定时按钮
    32. QPushButton *cancel; //取消按钮
    33. QLineEdit *clock; //设置闹钟
    34. QTextEdit *text; //设置播报的闹钟内容
    35. int timer_id;
    36. int timer_clock;
    37. QTextToSpeech *speecher; //播报员
    38. QPoint drap;
    39. };
    40. #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)
    6. {
    7. ui->setupUi(this);
    8. //窗口设置
    9. this->setFixedSize(600,300);
    10. this->setWindowTitle("Bloodborne alarm clock");
    11. this->setWindowIcon(QIcon(":/pricte/641193.jpg"));
    12. this->setWindowFlag(Qt::FramelessWindowHint); //纯净窗口
    13. //设置背景
    14. back = new QLabel(this);
    15. back->resize(this->size());
    16. back->setPixmap(QPixmap(":/pricte/R-C.jpg"));
    17. back->setScaledContents(true);
    18. //设置时间显示
    19. TIME = new QLabel(this);
    20. TIME->resize(350,40);
    21. QFont font("黑体",30,100);
    22. TIME->setStyleSheet("font-size:35px;"
    23. "color:white;");
    24. TIME->move(30,30);
    25. timer_id = this->startTimer(1000);
    26. //设置闹钟
    27. clock = new QLineEdit(this);
    28. clock->resize(120,30);
    29. clock->move(TIME->x()+430,TIME->y());
    30. clock->setPlaceholderText("设置闹钟");
    31. clock->setStyleSheet("background:rgb(0,0,0,0);"
    32. "color:white;");
    33. //定时按钮
    34. fixed_time = new QPushButton("定时",this);
    35. fixed_time->move(clock->x(),clock->y()+40);
    36. fixed_time->resize(clock->width()/2-5,25);
    37. fixed_time->setStyleSheet("background:rgb(102,255,255,80);"
    38. "color:white;");
    39. connect(fixed_time, &QPushButton::clicked, this, &Widget::fixed_time_clicked);
    40. //取消按钮
    41. cancel = new QPushButton("取消",this);
    42. cancel->move(clock->x()+fixed_time->width()+10,clock->y()+40);
    43. cancel->resize(clock->width()/2-5,25);
    44. cancel->setStyleSheet("background:rgb(102,255,255,80);"
    45. "color:white;");
    46. connect(cancel, &QPushButton::clicked, this, &Widget::cancel_clicked);
    47. cancel->setEnabled(false);
    48. //设置播报的闹钟内容
    49. text = new QTextEdit(this);
    50. text->resize(320,80);
    51. text->move(30,100);
    52. text->setStyleSheet("background:rgb(0,0,0,0);color:"
    53. "white;");
    54. text->setPlaceholderText("设置播报的闹钟内容");
    55. text->setFont(QFont("宋体",15));//设置字体和字体大小
    56. speecher = new QTextToSpeech(this);
    57. }
    58. Widget::~Widget()
    59. {
    60. delete ui;
    61. }
    62. //时间显示 时间比较
    63. void Widget::timerEvent(QTimerEvent *event)
    64. {
    65. QDateTime sys_dt = QDateTime::currentDateTime();
    66. if(event->timerId() == timer_id)
    67. {
    68. TIME->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss"));
    69. }
    70. if(event->timerId() == timer_clock)
    71. {
    72. QString a = clock->text();
    73. QString t = sys_dt.toString("hh:mm:ss");
    74. QString b = text->toPlainText();
    75. if(a == t)
    76. {
    77. speecher->say(b);
    78. }
    79. }
    80. }
    81. //定时按钮槽函数
    82. void Widget::fixed_time_clicked()
    83. {
    84. timer_clock = this->startTimer(1000);
    85. fixed_time->setEnabled(false);
    86. text->setEnabled(false);
    87. clock->setEnabled(false);
    88. cancel->setEnabled(true);
    89. }
    90. //取消按钮槽函数
    91. void Widget::cancel_clicked()
    92. {
    93. this->killTimer(timer_clock);
    94. fixed_time->setEnabled(true);
    95. text->setEnabled(true);
    96. clock->setEnabled(true);
    97. cancel->setEnabled(false);
    98. clock->clear();
    99. }
    100. //鼠标按下事件处理函数
    101. void Widget::mousePressEvent(QMouseEvent *event)
    102. {
    103. drap = event->globalPos()-this->pos();
    104. if(event->button() == Qt::RightButton)
    105. {
    106. this->close();
    107. }
    108. }
    109. //鼠标移动事件处理函数
    110. void Widget::mouseMoveEvent(QMouseEvent *event)
    111. {
    112. this->move(event->globalPos() - drap);
    113. }

    实现:

    思维导图

  • 相关阅读:
    java毕业设计春之梦理发店管理Mybatis+系统+数据库+调试部署
    Oracle 的开窗函数使用详解(一)
    最小安装CentOS7后安装Git2.37.2 220828记录
    linux问题总结2
    Linear Regression in mojo with NDBuffer
    消费升级下摩托车TBOX市场未来十年CAN数据应用发展趋势展望
    CSDN 重新开放付费资源的上传了,但要求如下
    Web基础与HTTP协议
    关于卷积神经网络的池化层(pooling)
    SpringBoot图片文件上传
  • 原文地址:https://blog.csdn.net/zjt020305/article/details/132650719