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> ...
随机推荐
- Mybatis Plus启动注入 SQL 原理分析
1) 问题: xxxMapper 继承了 BaseMapper<T>, BaseMapper 中提供了通用的 CRUD 方法, 方法来源于 BaseMapper, 有方法就必须有 SQL, ...
- Multiplication in PyTorch
1. Element-wise Multiplication * torch.Tensor.mul() torch.mul() 2. Matrix Multiplication torch.Tenso ...
- git add用法
git add命令是将工作区内容添加到暂存区.git commit 将暂存区内容添加到版本库. git add -A 提交所有变化 git add -u 提交被修改(modified)和被删除(d ...
- 用jQuery-Easy-UI编写注册页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 修改 Hue 默认数据库
Hue 更改默认数据 1. 需要安装 mysql 一下包 mysql-server mysql-devel mysql-shared mysql-client mysql-python shared ...
- RHS 和 LHS
不成功的的RHS 引用会导致抛出 ReferenceError异常 不成的的LHS 引用会导致自动隐式地创建一个全局变量(非严格模式下) function foo(a) { var b = a; ...
- ionic3 返回多个页面的写法
直接上代码 ionic3 返回2步 3步 或者多部 this.navCtrl.popTo(this.navCtrl.getByIndex(this.navCtrl.length()-3)); ...
- sizeof 用于返回一个对象或者类型所占据的内存数
整数类型 sizeof(int); 4字节or8字节 函数 sizeof(函数); 函数返回值类型占据的字节数 字符数组 char c[] = "abc"; sizeof(c); ...
- Elastix 2.4 双服务器热备搭建文档
一. 背景知识 本文档将会用到以下两个重要的组件: a) DRBD DRBD的全称为:Distributed Replicated Block Device,意为分布式块设备复制, D ...
- 3.Hadoop测试Yarn和MapReduce
Hadoop测试Yarn和MapReduce 1.配置Yarn (1)配置ResourceManager 生产环境中,一般是重开一台机器作为ResourceManager,这里我们以Master机器代 ...