当前位置:   article > 正文

qt day4

qt day4

设置一个时钟

 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. startTimer(1000);
  9. }
  10. Widget::~Widget()
  11. {
  12. delete ui;
  13. }
  14. //绘制时间处理函数的定义
  15. void Widget::paintEvent(QPaintEvent *event)
  16. {
  17. QPainter painter(this);
  18. // 绘制背景
  19. painter.fillRect(rect(), Qt::white);
  20. // 获取当前时间
  21. QDateTime currentTime = QDateTime::currentDateTime();
  22. int hour = currentTime.time().hour();
  23. int minute = currentTime.time().minute();
  24. int second = currentTime.time().second();
  25. // 绘制刻度
  26. painter.save();
  27. painter.translate(width() / 2, height() / 2); // 将坐标系原点移到中心点
  28. painter.scale(width() / 200.0, height() / 200.0); // 缩放坐标系
  29. // 绘制小时刻度
  30. painter.setPen(QPen(Qt::black, 4));
  31. for (int i = 0; i < 12; ++i) {
  32. painter.drawLine(80, 0, 96, 0);
  33. painter.rotate(30);
  34. }
  35. // 绘制分钟刻度
  36. painter.setPen(QPen(Qt::black, 2));
  37. for (int i = 0; i < 60; ++i) {
  38. if (i % 5 != 0) {
  39. painter.drawLine(88, 0, 96, 0);
  40. }
  41. painter.rotate(6);
  42. }
  43. painter.restore();
  44. // 绘制时钟指针
  45. painter.save();
  46. painter.translate(width() / 2, height() / 2);
  47. painter.scale(width() / 200.0, height() / 200.0);
  48. painter.setPen(QPen(Qt::black, 6));
  49. // 绘制小时指针
  50. painter.save();
  51. painter.rotate(hour * 30.0 + minute * 0.5); // 每小时30度,每分钟0.5度
  52. painter.drawLine(-10, 0, 40, 0);
  53. painter.restore();
  54. // 绘制分钟指针
  55. painter.save();
  56. painter.rotate(minute * 6.0 + second * 0.1); // 每分钟6度,每秒0.1度
  57. painter.drawLine(-10, 0, 60, 0);
  58. painter.restore();
  59. // 绘制秒钟指针
  60. painter.save();
  61. painter.rotate(second * 6.0); // 每秒6度
  62. painter.setPen(QPen(Qt::red, 1));
  63. painter.setBrush(Qt::red);
  64. painter.drawLine(-10, 0, 80, 0);
  65. painter.drawEllipse(-5, -5, 10, 10); // 绘制一个小圆点表示秒钟指针的末端
  66. painter.restore();
  67. painter.restore();
  68. }
  69. void Widget::timerEvent(QTimerEvent *event)
  70. {
  71. update();
  72. }

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QtDebug>
  5. #include <QPaintEvent>
  6. #include <QPainter>
  7. #include <QTimerEvent>
  8. #include <QTime>
  9. #include <QTimer>
  10. QT_BEGIN_NAMESPACE
  11. namespace Ui { class Widget; }
  12. QT_END_NAMESPACE
  13. class Widget : public QWidget
  14. {
  15. Q_OBJECT
  16. public:
  17. Widget(QWidget *parent = nullptr);
  18. ~Widget();
  19. //重写绘制事件处理函数
  20. void paintEvent(QPaintEvent *event) override;
  21. void timerEvent(QTimerEvent *event) override;
  22. private slots:
  23. //void timeout_slot();
  24. private:
  25. Ui::Widget *ui;
  26. QTimer *timer;
  27. };
  28. #endif // WIDGET_H

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/170615
推荐阅读
相关标签
  

闽ICP备14008679号