• Qt day4


    制作一个时钟

    源文件

    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. t_id=this->startTimer(1000);
    9. this->setWindowIcon(QIcon("C:/Users/WS/Pictures/1.png"));//改变左上角图标的位
    10. this->setWindowTitle("时钟");//窗口的名字
    11. this->setStyleSheet("background-color:pink");//背景
    12. }
    13. //定义绘制事件处理函数
    14. void Widget::paintEvent(QPaintEvent *event)
    15. {
    16. //实例化一个画家类
    17. QPainter p1(this);
    18. //实例化一个画笔使用无参构造
    19. QPen pen;
    20. //设置画笔颜色
    21. pen.setColor(QColor("brown"));
    22. //设置线性
    23. pen.setStyle(Qt::SolidLine);
    24. //设置画笔粗细
    25. pen.setWidth(5);
    26. //画笔给画家
    27. p1.setPen(pen);
    28. //设置画家坐标起点
    29. p1.translate(this->width()/2,this->height()/2);
    30. //设置画刷
    31. p1.setBrush(QBrush(QColor("pink")));
    32. //画一个圆圈
    33. p1.drawEllipse(QPointF(0,0),200,200);
    34. //颜色
    35. p1.setPen(QColor("brown"));
    36. //刻度
    37. for(int i=0;i<60;i++)
    38. {
    39. p1.drawLine(200,0,190,0);
    40. //旋转
    41. p1.rotate(6);
    42. }
    43. //绘画家设置字体
    44. p1.setFont(QFont("隶书",20));
    45. //1-12小时
    46. for(int i=1;i<=12;i++)
    47. {
    48. //旋转
    49. p1.rotate(30);
    50. p1.drawLine(0,-200,0,-180);
    51. p1.drawText(-10,-155,QString("%1").arg(i));
    52. }
    53. //画家2
    54. QPainter p2(this);
    55. p2.setPen(QColor("brown"));
    56. p2.translate(this->width()/2, this->height()/2);
    57. p2.rotate(-90);
    58. //画笔2
    59. QPen pen2;
    60. pen2.setWidth(3);
    61. pen2.setColor(QColor("red"));
    62. p2.setPen(pen2);
    63. //秒针
    64. p2.rotate(ss*6);
    65. p2.drawLine(0,0,150,0);
    66. //分针
    67. pen2.setColor(QColor("brown"));
    68. pen2.setWidth(5);
    69. p2.setPen(pen2);
    70. p2.rotate(-ss*6);
    71. p2.rotate(mm*6+ss*6/60);
    72. p2.drawLine(0,0, 150,0);
    73. //时针
    74. p2.rotate(-mm*6-ss*6/60);
    75. p2.rotate(hh*30+mm*6/12+ss*6/60/12);
    76. p2.drawLine(0,0, 100,0);
    77. }
    78. void Widget::timerEvent(QTimerEvent *event)
    79. {
    80. if(event->timerId()==t_id)
    81. {
    82. //数字显示当前时间
    83. QDateTime sysDateTime=QDateTime::currentDateTime();
    84. //讲QTime类对象转化为字符串
    85. QString t=sysDateTime.toString("hh:mm:ss");
    86. //展示到ui界面
    87. ui->nowtime->setText(t);
    88. //获取时分秒
    89. QStringList list =t.split(":");
    90. hh = list[0].toUInt();
    91. mm = list[1].toUInt();
    92. ss = list[2].toUInt();
    93. update();
    94. }
    95. }
    96. Widget::~Widget()
    97. {
    98. delete ui;
    99. }

    头文件

    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. void paintEvent(QPaintEvent *event) override;//画
    23. private:
    24. Ui::Widget *ui;
    25. int t_id;
    26. QTimer *timer;//显示屏显示时间
    27. int hh=0,mm=0,ss=0;
    28. };
    29. #endif // WIDGET_H

    pro

    1. QT += core gui
    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

  • 相关阅读:
    Qt作业五
    Adobe家里的“3D“建模工 | Dimension
    Docker 及 Docker Compose 安装指南
    在linux内核中如何定义一个byte变量? u8到底是什么类型
    深入理解Spring Security
    【Spark】Pyspark RDD
    [SQL开发笔记]INSERT INTO 语句:将新记录插入到数据库表中
    linux系统adb调试工具
    学内核之五:问题一,关于上下文切换
    Uniapp路由拦截-自定义路由白名单
  • 原文地址:https://blog.csdn.net/HYL1234511/article/details/133100472