重庆触摸屏 电子商务网站建设,网站建设的功能和目标,定制网站建设设计公司,百度免费下载安装1. 说明
在自定义时钟组件时#xff0c;使用到的基本控件主要是Canvas#xff0c;在绘制相关元素时有两种方式#xff1a;一种时在同一个canvas中绘制所有的部件元素#xff0c;这样需要不断的对画笔和画布的属性进行保存和恢复#xff0c;容易混乱#xff1b;另一种就是…1. 说明
在自定义时钟组件时使用到的基本控件主要是Canvas在绘制相关元素时有两种方式一种时在同一个canvas中绘制所有的部件元素这样需要不断的对画笔和画布的属性进行保存和恢复容易混乱另一种就是创建多个canvas组件每一部分的元素绘制都在各自的画布进行绘制逻辑比较清晰但是canvas组件会相对较多本文使用的是第二种方式。 效果展示
2. 整体代码
import QtQuick 2.15
import QtQuick.Controls 2.15Item{id:rootimplicitWidth: 400implicitHeight: implicitWidth// 尺寸属性property real outerCircleRadius:root.width / 2.05property real innerCircleRadius:root.width / 2.05// 颜色属性property color bgColor:whiteproperty color outerColor:blackproperty color innerColor:blackproperty color innerRootColor:lightSlateGrayproperty color innerLineColorL:#484D58property color innerLineColorS:#63677Aproperty color textColor:blackproperty color hourLineColor:#484D58property color minuteLineColor:#484D58property color secondLineColor:redproperty color timeTxtColor:black// 时间属性property var hoursproperty var minutesproperty var secondsproperty var currentTimeproperty alias hoursAngle:hourLine.angleproperty alias minutesAngle:minuteLine.angleproperty alias secondsAngle:secondLine.angle// 组件加载完成后先初始化当前时间Component.onCompleted: {calculateAngle()}// 时间计算function calculateAngle(){var date new Date()hours date.getHours()// 模除得到12小时制的小时数hours hours % 12minutes date.getMinutes()seconds date.getUTCSeconds()currentTime hours : minutes : secondshoursAngle Math.PI*2/12*hoursMath.PI*2*minutes/12/60-Math.PIminutesAngle Math.PI*2*minutes/60 Math.PI*2*seconds/60/60 -Math.PIsecondsAngle Math.PI*2*seconds/60-Math.PI}// 绘制背景Canvas{id:bgCirclewidth: root.widthheight: root.heightanchors.centerIn: parentonPaint: {// 绘制背景var ctx getContext(2d) ctx.save()ctx.lineWidth root.width/50 ctx.fillStyle bgColorctx.beginPath()ctx.arc(root.width/2,root.height/2,outerCircleRadius,0,Math.PI * (12/6))ctx.fill()ctx.restore()}}// 绘制圆环轮廓Canvas{id:outerCirclewidth: root.widthheight: root.heightanchors.centerIn: parentonPaint: {var ctx getContext(2d) //创建画师//为画师创建画笔并设置画笔属性ctx.lineWidth root.width/50 //设置画笔粗细ctx.strokeStyle outerColor //设置画笔颜色ctx.beginPath() //每次绘制调用此函数重新设置一个路径// 按照钟表刻度进行划分一圈是Math.PI * 2,分成12个刻度每个刻度占用 1/6// canvas绘制圆弧是按照顺时针绘制起点默认在三点钟方向ctx.arc(root.width/2,root.height/2,outerCircleRadius,0,Math.PI * (12/6))ctx.stroke() //根据strokeStyle对边框进行描绘}}// 绘制圆环内衬Canvas{id:innerCirclewidth: root.widthheight: root.heightanchors.centerIn: parentproperty real endAngle:Math.PI * (12/6)onPaint: {var ctx getContext(2d) ctx.save()ctx.lineWidth root.width/50 ctx.strokeStyle innerColor ctx.beginPath() ctx.arc(root.width/2,root.height/2,innerCircleRadius,0,endAngle)ctx.stroke() ctx.restore()// 绘制指针根部圆圈ctx.save()ctx.lineWidth root.width/50 ctx.fillStyle innerRootColorctx.beginPath()ctx.arc(root.width/2,root.height/2,innerCircleRadius/16,0,endAngle)ctx.fill()ctx.restore()}}// 绘制刻度线Canvas{id:innerLinewidth: root.widthheight: root.heightanchors.centerIn: parentproperty real lineNums:60onPaint: {var ctx getContext(2d) for (var i 0; i lineNums; i){ctx.beginPath();var angle 2 * Math.PI / 60 * i;var dx Math.cos(angle)*(outerCircleRadius-15);var dy Math.sin(angle)*(outerCircleRadius-15);var dx2 Math.cos(angle)*(outerCircleRadius-7);var dy2 Math.sin(angle)*(outerCircleRadius-7);if (i % 5 0){ctx.lineWidth root.width/100ctx.strokeStyle innerLineColorL}else{ctx.lineWidth root.width/200ctx.strokeStyle innerLineColorS}ctx.moveTo(root.width/2dx,root.height/2dy);ctx.lineTo(root.width/2dx2,root.height/2dy2);ctx.stroke();}}}// 绘制数字Canvas{id:drawTextwidth: root.widthheight: root.heightanchors.centerIn: parentproperty var numbers : [1,2,3,4,5,6,7,8,9,10,11,12]onPaint: {var ctx getContext(2d) ctx.font 18px Arial;ctx.textAlign center;ctx.textBaseline middle;for(var i 0; i 12; i){ctx.fillStyle textColorvar angle 2 * Math.PI / 12 * numbers[i] - 3.14 / 2;var dx Math.cos(angle)*(outerCircleRadius-30);var dy Math.sin(angle)*(outerCircleRadius-30);ctx.fillText(numbers[i],root.width/2 dx,root.height / 2 dy);ctx.fill()}}}// 绘制时针线Canvas{id:hourLinewidth: root.widthheight: root.heightanchors.centerIn: parentproperty real angleonPaint: {var ctx getContext(2d) // 先清空画布上之前的内容ctx.clearRect(0,0,width,height)ctx.save()ctx.beginPath()ctx.lineWidth root.width/100ctx.strokeStylehourLineColor// 平移坐标点(注意坐标系原点先平移再旋转)ctx.translate(root.width/2,root.height/2)// 旋转坐标系ctx.rotate(angle)// 坐标原点变化之后再进行实际的绘图ctx.moveTo(0,-20);ctx.lineTo(0,outerCircleRadius / 2 - 15);ctx.stroke()ctx.restore()}}// 绘制分针线Canvas{id:minuteLinewidth: root.widthheight: root.heightanchors.centerIn: parentproperty real angleonPaint: {var ctx getContext(2d) ctx.clearRect(0,0,width,height)ctx.save()ctx.beginPath()ctx.lineWidth root.width/100ctx.strokeStyleminuteLineColor// 平移坐标点(注意坐标系原点先平移再旋转)ctx.translate(root.width/2,root.height/2)// 旋转坐标系ctx.rotate(angle)// 坐标原点变化之后再进行实际的绘图ctx.moveTo(0,-25);ctx.lineTo(0,outerCircleRadius / 2 - 5);ctx.stroke()ctx.restore()}}// 绘制秒针线Canvas{id:secondLinewidth: root.widthheight: root.heightanchors.centerIn: parentproperty real angleonPaint: {var ctx getContext(2d) ctx.clearRect(0,0,width,height)ctx.save()ctx.beginPath()ctx.lineWidth root.width/100ctx.strokeStylesecondLineColor// 平移坐标点(注意坐标系原点先平移再旋转)ctx.translate(root.width/2,root.height/2)// 旋转坐标系ctx.rotate(angle)// 坐标原点变化之后再进行实际的绘图ctx.moveTo(0,-30);ctx.lineTo(0,outerCircleRadius / 1.5);ctx.stroke()ctx.restore()}}// 当前时间显示Text{id:timeTxty:root.height * 0.75anchors.horizontalCenter: root.horizontalCentertext: currentTimefont.pixelSize: root.width/12color: timeTxtColor}// 使用定时器(每秒钟进行计算)Timer{id:angleCalinterval: 1000repeat: truerunning: trueonTriggered: {calculateAngle()hourLine.requestPaint()minuteLine.requestPaint()secondLine.requestPaint()}}
} 文章转载自: http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.jfqqs.cn.gov.cn.jfqqs.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.bbyqz.cn.gov.cn.bbyqz.cn http://www.morning.paoers.com.gov.cn.paoers.com http://www.morning.zdgp.cn.gov.cn.zdgp.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.rbnp.cn.gov.cn.rbnp.cn http://www.morning.pflry.cn.gov.cn.pflry.cn http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.8yitong.com.gov.cn.8yitong.com http://www.morning.rzmlc.cn.gov.cn.rzmlc.cn http://www.morning.ktcrr.cn.gov.cn.ktcrr.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.ckxd.cn.gov.cn.ckxd.cn http://www.morning.mwwnz.cn.gov.cn.mwwnz.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.nwgkk.cn.gov.cn.nwgkk.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.gltmz.cn.gov.cn.gltmz.cn http://www.morning.hpprx.cn.gov.cn.hpprx.cn http://www.morning.pkrb.cn.gov.cn.pkrb.cn http://www.morning.zbkwj.cn.gov.cn.zbkwj.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.cniedu.com.gov.cn.cniedu.com http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.tbhlc.cn.gov.cn.tbhlc.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.xknmn.cn.gov.cn.xknmn.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.zcqbx.cn.gov.cn.zcqbx.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.dkbgg.cn.gov.cn.dkbgg.cn http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn http://www.morning.jxfmn.cn.gov.cn.jxfmn.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.tlnbg.cn.gov.cn.tlnbg.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn