先看下演示效果

大致过程

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

剩下再绘制三个指针

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

下面写源代码

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")
}
} }

Qt-QML-Canvas写个小小的闹钟的更多相关文章

  1. qt qml中PropertyAnimation的几种使用方法

    qml文章 qt qml中PropertyAnimation的几种使用方法 动画应用场景有以下几种: 首先如果一个Rectangle.动画是要改变它的x和y值 1,Rectangle一旦被创建,就要移 ...

  2. Qt QML referenceexamples attached Demo hacking

    /********************************************************************************************* * Qt ...

  3. Qt qml的软件架构设计

    google: qt qml application architecture 有很多资源. 1 https://www.ics.com/blog/multilayered-architecture- ...

  4. 用Canvas写桌球游戏!!!

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 昨天上班的时候闲着无事,就用Canvas写了个桌球游戏来玩玩....所以就拿这游戏上来水一发.或许对一些刚学canvas的人有帮助. 话说 ...

  5. Qt qml 单例模式

    Qt qml 单例模式,没什么好说的,看代码吧.单例模式很适合做全局的配置文件. [示例下载] http://download.csdn.net/detail/surfsky/8539313 [以下是 ...

  6. Qt qml listview 列表视图控件(下拉刷新、上拉分页、滚动轴)

    Qt qml listview下拉刷新和上拉分页主要根据contentY来判断.但要加上顶部下拉指示器.滚动条,并封装成可简单调用的组件,着实花了我不少精力:) [先看效果]    [功能] 下拉刷新 ...

  7. qt qml qchart 图表组件

    qt qml qchart 图表组件 * Author: Julien Wintz * Created: Thu Feb 13 23:41:59 2014 (+0100) 这玩意是从chart.js迁 ...

  8. qt qml fuzzyPanel 毛玻璃效果

    毛玻璃效果,用qml来写代码真是简短,大爱qml:) [下载地址]http://download.csdn.net/detail/surfsky/8426641 [核心代码] Rectangle{ c ...

  9. canvas写的一个小时钟demo

    <!DOCTYPE html> <html> <head> <title>HTML5 Canvas Demo of clock</title> ...

随机推荐

  1. WiFi密码忘记了怎么办之解决方案

    随着科技不断进步,网络产品也越来越便宜了.家家户户基本上都有能力装上宽带. 但是有的时候,时间久了,我们可能会忘记密码(密码设置比较复杂的情况下).那么如何找到密码呢? 通常的办法有很多,百度或者Go ...

  2. JSP基本指令

    jsp命令指令用来设置与整个jsp页面相关的属性,它并不直接产生任何可见的输出,而只是告诉引擎如何处理其余JSP页面.其一般语法形式为: <%@ 指令名称 属性=“值”%> 三种命令指令分 ...

  3. SSM衍生的配置文件

    JDBC:(Java database connectivity) 目的:将Java语言和数据库解耦和,使得一套Java程序能对应不同的数据库. 方法:sun公司制定了一套连接数据库的接口(API). ...

  4. android学习:Android上面部署Apache FTPServer

    经过了几天的研究,终于Apache FTPServer在Android的配置和使用上有了一些心得,现在分享出来,提供给大家参考,说到这儿又不得不吐槽一下这要命的转载了,找Apache FTPServe ...

  5. vue 父子组件的生命周期顺序

    一.加载渲染过程 父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount ...

  6. Delphi Android下包含第三方DEX

    1.将jar转换为dex call dx --dex -verbose --output=.\output\dex\test_classes.dex --positions=lines .\outpu ...

  7. 通过XShell实现windows文件上传到Linux服务器上

    .XShell上传文件到Linux服务器上 在学习Linux过程中,我们常常需要将本地文件上传到Linux主机上,这里简单记录下使用Xsheel工具进行文件传输 1:首先连接上一台Linux主机 2: ...

  8. pci枚举初始化部分(2)

    1.2.8判断pcie设备是否支持雷电技术 Intel具有一种基于Thunderbolt技术的PCIE变体,它结合了DisplayPort和PCIe协议,与Mini DisplayPort兼容. Th ...

  9. LFS搭建第一天

    1. 前期准备 vmware 软件安装 LFS iso 下载:http://ftp.osuosl.org/pub/lfs-livecd/lfslivecd-x86-6.3-r2145.iso 2.新建 ...

  10. Asp.Ner Core定时任务

    AspNet Core定时任务 纪念人类首张黑洞照片发布 第一种方式BackgroundService 基于后台服务类BackgroundService实现,类所在命名空间Microsoft.Exte ...