• qt作业day4


    1. //clock_exercise.cpp
    2. #include "clock_timer.h"
    3. #include "ui_clock_timer.h"
    4. //时间事件处理函数
    5. void Clock_Timer::timerEvent(QTimerEvent *event)
    6. {
    7. if(event->timerId() == time_id)
    8. {
    9. sys_tm = QDateTime :: currentDateTime();
    10. // int year = sys_tm.date().year();
    11. // int month = sys_tm.date().month();
    12. // int day = sys_tm.date().day();
    13. //时分秒
    14. int hour = sys_tm.time().hour();
    15. int min = sys_tm.time().minute();
    16. int sec = sys_tm.time().second();
    17. // qDebug() << "系统时间:" << hour << ":" << min << ":" << sec;
    18. QString settime = timeEdit->text();
    19. this->timeLab->setText(sys_tm.toString("yyyy-mm-dd hh:mm:ss"));
    20. splitTime();
    21. QString readtxt =this->txt_edit->toPlainText();
    22. vector<int> :: iterator iter = split.begin();
    23. if(*(iter++) == hour && *(iter++) == min && *iter == sec)
    24. {
    25. speech->say(this->txt_edit->toPlainText());
    26. }
    27. }
    28. }
    29. Clock_Timer::Clock_Timer(QWidget *parent) :
    30. QWidget(parent),
    31. ui(new Ui::Clock_Timer)
    32. {
    33. ui->setupUi(this);
    34. this->resize(800,700);
    35. this->setWindowFlags(Qt :: FramelessWindowHint);
    36. // this->setWindowTitle("小淼子快起床了");
    37. //显示系统时间标签
    38. timeLab = new QLabel(this);
    39. timeLab->move(30,20);
    40. timeLab->resize(300,60);
    41. timeLab->setText("系统时间");
    42. timeLab->setFont(QFont("楷体",16));
    43. timeLab->setStyleSheet("background-color:skyblue");
    44. //启动按钮
    45. startBtn = new QPushButton(this);
    46. startBtn->setText("启动");
    47. startBtn->move(600,50);
    48. startBtn->resize(QSize(60,40));
    49. connect(startBtn,&QPushButton :: clicked,this,&Clock_Timer :: startRecTime);
    50. //停止按钮
    51. stopBtn = new QPushButton(this);
    52. stopBtn->resize(startBtn->size());
    53. stopBtn->move(startBtn->x() + startBtn->width() + 20,startBtn->y());
    54. stopBtn->setText("停止");
    55. stopBtn->setEnabled(false);
    56. connect(stopBtn,&QPushButton :: clicked,this,&Clock_Timer :: stopRecTime);
    57. //用户输入时间单行编辑器
    58. timeEdit = new QLineEdit(this);
    59. timeEdit->move(400,timeLab->y());
    60. timeEdit->setFont(QFont("楷体",16));
    61. timeEdit->resize(200,80);
    62. timeEdit->setPlaceholderText("时:分:秒");
    63. //用户输入语音播报文本编辑器
    64. txt_edit = new QTextEdit(this);
    65. txt_edit->move(0,200);
    66. txt_edit->resize(QSize(this->width(),this->height() - 200));
    67. txt_edit->setFont(QFont("楷体",20));
    68. //语音播报
    69. speech = new QTextToSpeech(this);
    70. }
    71. void Clock_Timer::mouseMoveEvent(QMouseEvent *event)
    72. {
    73. if(event->buttons() == Qt :: LeftButton)
    74. {
    75. this->move(event->globalPos() - startPos);
    76. }
    77. }
    78. void Clock_Timer::mousePressEvent(QMouseEvent *event)
    79. {
    80. if(event->buttons() == Qt :: LeftButton)
    81. {
    82. this->startPos = event->globalPos() - this->pos();
    83. }
    84. else if(event->buttons() == Qt :: RightButton)
    85. {
    86. int ret = QMessageBox :: question(this,"确认","是否要关闭界面",QMessageBox :: Yes | QMessageBox :: No,QMessageBox :: No);
    87. if(ret == QMessageBox :: Yes)
    88. {
    89. this->close();
    90. }
    91. }
    92. }
    93. Clock_Timer::~Clock_Timer()
    94. {
    95. delete ui;
    96. }
    97. //按下启动按钮后的事件处理函数
    98. void Clock_Timer::startRecTime()
    99. {
    100. // qDebug() << this->timeEdit->text();
    101. if(this->timeEdit->text() == "")
    102. {
    103. QMessageBox :: warning(this,"警告","无输入",QMessageBox :: Ok,QMessageBox :: Ok);
    104. return;
    105. }
    106. //点击启动后,解锁停止按钮,同时锁定启动按钮
    107. this->stopBtn->setEnabled(true);
    108. this->startBtn->setEnabled(false);
    109. this->timeEdit->setEnabled(false);
    110. this->txt_edit->setEnabled(false);
    111. //开启定时器
    112. time_id = this->startTimer(1000);
    113. }
    114. //按下停止按钮后的事件处理函数
    115. void Clock_Timer::stopRecTime()
    116. {
    117. int ret = QMessageBox :: question(this,"提示","是否确认要停止定时器",QMessageBox :: Yes | QMessageBox :: No,QMessageBox :: Yes);
    118. if(ret == QMessageBox :: Yes)
    119. {
    120. this->startBtn->setEnabled(true);
    121. this->stopBtn->setEnabled(false);
    122. this->timeEdit->setEnabled(true);
    123. this->txt_edit->setEnabled(true);
    124. //关闭定时器
    125. this->killTimer(time_id);
    126. }
    127. }
    128. //分割时分秒
    129. void Clock_Timer :: splitTime()
    130. {
    131. this->split.clear();
    132. QString temp = timeEdit->text();
    133. QStringList s = temp.split(":");
    134. QStringList :: iterator iter = s.begin();
    135. for(;iter != s.end();iter++)
    136. {
    137. qDebug() << (*iter);
    138. split.push_back(iter->toInt());
    139. }
    140. }
    1. //头文件
    2. #ifndef CLOCK_TIMER_H
    3. #define CLOCK_TIMER_H
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. using namespace std;
    17. namespace Ui {
    18. class Clock_Timer;
    19. }
    20. class Clock_Timer : public QWidget
    21. {
    22. Q_OBJECT
    23. void timerEvent(QTimerEvent *event); //时间事件处理函数
    24. public:
    25. explicit Clock_Timer(QWidget *parent = nullptr);
    26. void mouseMoveEvent(QMouseEvent *event);
    27. void mousePressEvent(QMouseEvent *event);
    28. ~Clock_Timer();
    29. void splitTime();
    30. public slots:
    31. void startRecTime();
    32. void stopRecTime();
    33. private:
    34. Ui::Clock_Timer *ui;
    35. QPushButton *startBtn; //启动按钮
    36. QPushButton *stopBtn; //停止按钮
    37. QLabel *timeLab; //系统获取时间
    38. QLineEdit *timeEdit; //记录用户输入的时间
    39. QTextEdit *txt_edit; //用户输入的播报内容
    40. QMessageBox *stopConfirm; //对话框,询问用户是否确认停止
    41. QTextToSpeech *speech;
    42. int time_id; //记录定时器ID
    43. QDateTime sys_tm; //记录系统时间
    44. vector<int> split;
    45. QPoint startPos; //记录鼠标起始点
    46. };
    47. #endif // CLOCK_TIMER_H

    运行效果

  • 相关阅读:
    stream流中 filter方法自定义过滤方式
    设计模式之适配器模式:接口对接丝般顺滑(图代码解析面面俱到)
    第十三章,枚举与泛型
    RK3399平台开发系列讲解(ALSA子系统)4.37、ALSA驱动框架
    字符流用户注册案例、字符缓冲流、字符缓冲流特有功能、字符缓冲流操作文件中的数据排序案例
    自动化测试框架
    SpringMVC的统一处理
    `算法知识` 欧拉定理, 费马小定理
    【wp】2023鹏城杯初赛 Web web1(反序列化漏洞)
    《大学“电路分析基础”课程实验合集.实验三》丨基尔霍夫定律的验证
  • 原文地址:https://blog.csdn.net/a136630108/article/details/132661443