Qt-QML-Canvas写个小小的闹钟

简介: Qt-QML-Canvas写个小小的闹钟

image.png

先看下演示效果


大致过程



先绘制仪表盘,圆圈和刻度


剩下再绘制三个指针


最后在绘制上面的电子时钟


下面写源代码


import QtQuick 2.0


Rectangle

{

   id:root

   anchors.centerIn: parent

   width: 200

   height: 200

   color: "transparent"

   property int time_H : 0;

   property int time_M : 0;

   property int time_S : 0;



   Timer

   {

           interval: 500; running: true; repeat: true

           onTriggered:

           {


               time.text = Qt.formatDateTime(new Date(), "hh:mm:ss")

               root.time_H = Qt.formatDateTime(new Date(), "hh")

               root.time_M = Qt.formatDateTime(new Date(), "mm")

               root.time_S = Qt.formatDateTime(new Date(), "ss")

           }


   }


   Canvas

   {

       anchors.fill: parent

       onPaint:

       {

           var ctx = getContext("2d");

           ctx.lineWidth = 2;

           ctx.strokeStyle = "#FFFFFF";

           ctx.globalAlpha = 1.0;

           ctx.beginPath();

           ctx.arc(100,100,99,0,2*Math.PI);

           ctx.stroke();

           ctx.restore();


           for(var i =0;i<32;i++)

           {

               ctx.save();

               ctx.translate(width/2,height/2);

               ctx.rotate(((i)*30)*Math.PI/180);

               ctx.beginPath();

               //判定长短线

               if(i%3 == 0)

               {

                   ctx.moveTo(0,-99+15);

                   ctx.lineTo(0,-99+2);

               }

               else

               {

                   ctx.moveTo(0,-99+10);

                   ctx.lineTo(0,-99+2);

               }

               //绘制

               ctx.stroke();

               ctx.restore();

           }

       }

   }

   Rectangle

   {

       width: 20

       height: 20

       anchors.horizontalCenter: parent.horizontalCenter

       anchors.top: parent.top

       anchors.topMargin: 15

       color: "#00000000"

       Text {

           anchors.centerIn: parent

           font.family: "微软雅黑"

           font.pixelSize: 14

           color: "#FFFFFF"

           text: qsTr("12")

       }

   }

   Rectangle

   {

       width: 20

       height: 20

       anchors.verticalCenter: parent.verticalCenter

       anchors.right: parent.right

       anchors.rightMargin: 15

       color: "#00000000"

       Text {

           anchors.centerIn: parent

           font.family: "微软雅黑"

           font.pixelSize: 14

           color: "#FFFFFF"

           text: qsTr("3")

       }

   }

   Rectangle

   {

       width: 20

       height: 20

       anchors.verticalCenter: parent.verticalCenter

       anchors.left: parent.left

       anchors.leftMargin: 15

       color: "#00000000"

       Text {

           anchors.centerIn: parent

           font.family: "微软雅黑"

           font.pixelSize: 14

           color: "#FFFFFF"

           text: qsTr("9")

       }

   }

   Rectangle

   {

       width: 20

       height: 20

       anchors.horizontalCenter: parent.horizontalCenter

       anchors.bottom: parent.bottom

       anchors.bottomMargin: 15

       color: "#00000000"

       Text {

           anchors.centerIn: parent

           font.family: "微软雅黑"

           font.pixelSize: 14

           color: "#FFFFFF"

           text: qsTr("6")

       }

   }

   Canvas

   {

       id:m_Hour

       property int m_Radius: (root.width>=root.height)? (root.height)/2 : (root.width)/2

       width: m_Radius*2

       height: m_Radius*2

       anchors.centerIn: parent

       rotation: (root.time_H%12)*30 + root.time_M*6/60 + root.time_S*6/60/60 - 180

       onPaint:

       {

           var ctx = getContext("2d");

           ctx.lineWidth = 2;

           ctx.fillStyle = "#FFFFFF";

           ctx.strokeStyle = "#FFFFFF";

           ctx.beginPath()

           ctx.moveTo(m_Radius,m_Radius)

           ctx.lineTo(m_Radius+4,m_Radius+m_Radius*0.5)

           ctx.lineTo(m_Radius,m_Radius*2-37)

           ctx.lineTo(m_Radius-4,m_Radius+m_Radius*0.5)

           ctx.closePath()

           ctx.fill()

           ctx.stroke()

       }


   }

   Canvas

   {

       id:m_Min

       property int m_Radius: (root.width>=root.height)? (root.height)/2 : (root.width)/2

       width: m_Radius*2

       height: m_Radius*2

       anchors.centerIn: parent

       rotation: root.time_M*6 + root.time_S*6/60 - 180

       onPaint:

       {

           var ctx = getContext("2d");

           ctx.lineWidth = 2;

           ctx.fillStyle = "yellow";

           ctx.strokeStyle = "yellow";

           ctx.beginPath()

           ctx.moveTo(m_Radius,m_Radius)

           ctx.lineTo(m_Radius+2,m_Radius+m_Radius*0.5)

           ctx.lineTo(m_Radius,m_Radius*2-32)

           ctx.lineTo(m_Radius-2,m_Radius+m_Radius*0.5)

           ctx.closePath()

           ctx.fill()

           ctx.stroke()

       }


   }


   Canvas

   {

       id:m_Second

       property int m_Radius: (root.width>=root.height)? (root.height)/2 : (root.width)/2

       width: m_Radius*2

       height: m_Radius*2

       anchors.centerIn: parent

       rotation: root.time_S*6 - 180

       onPaint:

       {

           var ctx = getContext("2d");

           ctx.lineWidth = 2;

           ctx.fillStyle = "red";

           ctx.strokeStyle = "red";

           ctx.beginPath()

           ctx.moveTo(m_Radius,m_Radius)

           ctx.lineTo(m_Radius+1,m_Radius+m_Radius*0.5)

           ctx.lineTo(m_Radius,m_Radius*2-28)

           ctx.lineTo(m_Radius-1,m_Radius+m_Radius*0.5)

           ctx.closePath()

           ctx.fill()

           ctx.stroke()

       }


   }

   Rectangle

   {

       border.color: "#FFFFFF"

       border.width: 2

       width: 80

       height: 40

       anchors.centerIn: parent

       color: "#000000"

       Text {

           id:time

           anchors.centerIn: parent

           color: "#00FF00"

           font.family: "微软雅黑"

           font.pixelSize: 16

           text: qsTr("22:22:22")

       }

   }


}


目录
相关文章
|
9月前
|
编解码 容器
QML/Qt Quick anchors.fill 的使用(二)
QML/Qt Quick anchors.fill 的使用
236 0
|
程序员 C语言
Qt编写自定义控件49-飞机仪表盘
一、前言 飞行仪表是测定和表示飞机数据的工具,飞机中必不可少的一部分,飞行员根据飞行仪表表示的数据才能正确地做出判断。一般飞机仪表包括高度表+空速表+垂直速率表+姿态仪+航向指示表+转弯协调表。这次要绘制的是其中的姿势仪,显示飞机相对于地平线的姿态,看姿态仪,飞行员能判断飞机姿态为偏左偏右,及偏上和偏下。
1774 0
|
8月前
|
算法 JavaScript 前端开发
在JavaScript中实现基本的碰撞检测算法,我们通常会用到矩形碰撞检测,也就是AABB(Axis-Aligned Bounding Box)碰撞检测
【6月更文挑战第16天】JavaScript中的基本碰撞检测涉及AABB(轴对齐边界框)方法,常用于2D游戏。`Rectangle`类定义了矩形的属性,并包含一个`collidesWith`方法,通过比较边界来检测碰撞。若两矩形无重叠部分,四个条件(关于边界相对位置)均需满足。此基础算法适用于简单场景,复杂情况可能需采用更高级的检测技术或物理引擎库。
133 6
|
4月前
|
Linux 视频直播
FFmpeg开发笔记(五十四)使用EasyPusher实现移动端的RTSP直播
本文介绍了如何使用EasyPusher-Android实现RTSP直播流程。首先对比了RTSP、RTMP、SRT和RIST四种流媒体协议,并以RTSP为例,详细说明了使用EasyPusher-Android向流媒体服务器进行RTSP直播推流的方法。文中还提供了OBS Studio配置RTSP插件及ZLMediaKit云服务器部署的相关信息,通过修改EasyPusher-Android源码使其支持通用RTSP地址,最终验证了直播功能的成功实现。
125 0
FFmpeg开发笔记(五十四)使用EasyPusher实现移动端的RTSP直播
|
6月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
198 2
|
9月前
|
存储 安全 程序员
【C++ 包装器类 智能指针】完全教程:std::unique_ptr、std::shared_ptr、std::weak_ptr的用法解析与优化 — 初学者至进阶指南
【C++ 包装器类 智能指针】完全教程:std::unique_ptr、std::shared_ptr、std::weak_ptr的用法解析与优化 — 初学者至进阶指南
328 0
|
9月前
|
网络协议 安全 网络安全
Qt 套接字类(QTcpSocket和QUdpSocket)解密:迈向 Qt 网络编程之巅
Qt 套接字类(QTcpSocket和QUdpSocket)解密:迈向 Qt 网络编程之巅
1514 0
|
9月前
|
编译器 Python
【python】控制台中文输出乱码解决方案
【python】控制台中文输出乱码解决方案
242 0
使用QT的QProcess执行cmd命令【记录】
使用QT的QProcess执行cmd命令【记录】
1590 0
|
Java Android开发 Kotlin
Android转AndroidX
公司项目多,有些事java,有些是kotlin,有些是Android,有些是AndroidX,一些依赖和依赖库的更新使得调用的方法等不同,导致功能同步时效率及极其低下,于是打算把Android项目转成AndroidX。

热门文章

最新文章