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> ...
随机推荐
- java的四舍五入
四舍五入是我们小学的数学问题,这个问题对于我们程序猿来说就类似于1到10的加减乘除那么简单了.在讲解之间我们先看如下一个经典的案例: public static void main(String[] ...
- [Python]json对象转换出错expected string or buffer python
[问题] 今天在使用python中的json转换碰到一个问题: 错误显示: expected string or buffer json内容如下: {u'err_no': 0, u'corpus_no ...
- 使用Apache HttpClient 4.5设置超时时间
使用HttpClient,一般都需要设置连接超时时间和获取数据超时时间.这两个参数很重要,目的是为了防止访问其他http服务时,由于超时导致自己的应用受影响. 4.5版本中,这两个参数的设置都抽象到了 ...
- HDU 1301Jungle Roads(最小生成树 prim,输入比较特殊)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1301 Jungle Roads Time Limit: 2000/1000 MS (Java/Oth ...
- linux SVN添加新用户
首先找到用户文件:authz.conf; 用vi 编辑authz.conf文件在develps 后面添加你要添加的用户名:如图: 上图:cheny就是我后面添加上去的用户名 按Esc :wq保存au ...
- Angular7教程-02-Angular项目目录及基本文件说明
本教程基于Angular7,更新时间2018-11-05. 1. 项目根目录如下: e2e文件夹:end to end,测试目录,主要用于集成测试. node_modules:项目的模块依赖目录. s ...
- Archlinux+gnome安装中文输入法
环境:archlinux+gnome 1.首先需要配置Archlinuxcn源 打开/etc/pacman.conf,添加 [archlinuxcn] Server = https://mirrors ...
- Python-逻辑运算
1 or 3>2 and 4<5 or 6 and 2<7
- mitmproxy 在windows上的使用
mitmproxy 是一个中间件代理, 结合python使用 安装 pip install mitmproxy 在windows上没有mitmproxy 所以只要用mitmdump和mitmwdb ...
- python的MetaClass的代码分析。基于廖雪峰博客代码
# 一张表一个类,表内每一行就是一个实例 ''' 一个单独的元类使用的程序分析. ''' class Field(object): def __init__(self, name, column_ty ...