Qt-QML-Canvas写个小小的闹钟
先看下演示效果
大致过程
先绘制仪表盘,圆圈和刻度
剩下再绘制三个指针
最后在绘制上面的电子时钟
下面写源代码
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写个小小的闹钟的更多相关文章
- qt qml中PropertyAnimation的几种使用方法
qml文章 qt qml中PropertyAnimation的几种使用方法 动画应用场景有以下几种: 首先如果一个Rectangle.动画是要改变它的x和y值 1,Rectangle一旦被创建,就要移 ...
- Qt QML referenceexamples attached Demo hacking
/********************************************************************************************* * Qt ...
- Qt qml的软件架构设计
google: qt qml application architecture 有很多资源. 1 https://www.ics.com/blog/multilayered-architecture- ...
- 用Canvas写桌球游戏!!!
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 昨天上班的时候闲着无事,就用Canvas写了个桌球游戏来玩玩....所以就拿这游戏上来水一发.或许对一些刚学canvas的人有帮助. 话说 ...
- Qt qml 单例模式
Qt qml 单例模式,没什么好说的,看代码吧.单例模式很适合做全局的配置文件. [示例下载] http://download.csdn.net/detail/surfsky/8539313 [以下是 ...
- Qt qml listview 列表视图控件(下拉刷新、上拉分页、滚动轴)
Qt qml listview下拉刷新和上拉分页主要根据contentY来判断.但要加上顶部下拉指示器.滚动条,并封装成可简单调用的组件,着实花了我不少精力:) [先看效果] [功能] 下拉刷新 ...
- qt qml qchart 图表组件
qt qml qchart 图表组件 * Author: Julien Wintz * Created: Thu Feb 13 23:41:59 2014 (+0100) 这玩意是从chart.js迁 ...
- qt qml fuzzyPanel 毛玻璃效果
毛玻璃效果,用qml来写代码真是简短,大爱qml:) [下载地址]http://download.csdn.net/detail/surfsky/8426641 [核心代码] Rectangle{ c ...
- canvas写的一个小时钟demo
<!DOCTYPE html> <html> <head> <title>HTML5 Canvas Demo of clock</title> ...
随机推荐
- Owin+ASP.NET Identity浅析系列(四)实现用户角色
在今天,读书有时是件“麻烦”事.它需要你付出时间,付出精力,还要付出一份心境.--仅以<Owin+ASP.NET Identity浅析系列>来祭奠那逝去的…… 通过Owin+ASP.NET ...
- face++
1.链表反转 2.快排 3.m*k n*k两矩阵计算欧几里得距离np.tile 4.链表排序,要求时间复杂度小于O(N^2),空间O(1),不允许改变链表的值 5.2sum及其变体 6.给一个数组 ...
- windows系统安装erlang
1.什么是erlang erlang是一种通用的面向并发的编程语言,它由瑞典电信设备制造商爱立信所辖的CS-Lab开发,目的是创造一种可以应对大规模并发活动的编程语言和运行环境.Erlang是一个结构 ...
- geomesa hbase geoserver
在geoserver中配置hbase ln -s /root/hbase/hbase-1.4.8/conf/hbase-site.xml /root/tomcat/apache-tomcat-7.0. ...
- PAT——1043. 输出PATest
给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按“PATestPATest....”这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符已 ...
- angular setInterval计时操作
在angular中setInterval方法是单向绑定,只绑定一次,无法实现计时效果, 可以使用$interval实现.附上代码:
- (解释文)My SQL中主键为0和主键自排约束的关系
上一篇我们说了关于自排如果主键是0的问题,在这里我搞清楚了原因,导致这种情况是因为在SQL中对自排设置了初始值: 从这里可以看到这两个变量一个是自增的初始值,一个是增量,这里都是1,所以在设置自增的时 ...
- MongoDB DBA 实践4-----创建复制集
一.复制 复制允许多个数据库服务器共享相同数据的功能,从而确保冗余并促进负载平衡 1.数据冗余及可用性 复制技术提供数据冗余及可用性,在不同的数据库服务器上使用多个数据副本,复制技术防止单个数据库服务 ...
- 大数据时代数据库-云HBase架构&生态&实践
业务的挑战 存储量量/并发计算增大 现如今大量的中小型公司并没有大规模的数据,如果一家公司的数据量超过100T,且能通过数据产生新的价值,基本可以说是大数据公司了 .起初,一个创业公司的基本思路就是首 ...
- s3c6410 RomCode文档读后总结
最近无意中看到一篇关于s3c6410 RomCode的介绍,结合自己的经验,做个总结. 首先贴张图,具体描述下该芯片的启动方式及具体流程. 因为s3c6410的板子多数是从SD或者Nand方式启动,重 ...