qml中使用canvas绘制温度计

网上只找到一篇vip文章,没钱充会员,只能自己画了

使用环境QT5.12.7
温度计

main.qml代码如下

// An highlighted block
import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 400
    height: 680
    title: qsTr("Hello World")

    Rectangle {
        id: rect1
        width: 200
        height: 600
        anchors.centerIn: parent
        color: "gray"
        Canvas{
            id:myCanvas
            width: parent.width
            height: parent.height
            anchors.fill: parent
            onPaint: {
                var ctx = getContext("2d");

                //起始角度,结束角度
                var beginAng = 1.35*Math.PI
                var endAng = 1.65*Math.PI

                var allH = 500//圆和柱连接点高
                var allw = 100//柱顶端高
                var r = 10//圆半径

                var cT = 0;//当前温度


                var minTemperature = -50 //最小温度
                var maxTemperature = 110 //最大温度

                var intervalGrid = (parseInt(maxTemperature/10)-parseInt(minTemperature/10))*2 //从最小值到最大值有多少个格子
                var intervalDistanse = (400-50)/intervalGrid //每个格子的距离

                cT = cT>maxTemperature?maxTemperature:(cT<minTemperature?minTemperature:cT) // 限制温度计超出

                var currentTemperature = allH+r*Math.cos(120-beginAng)-(10 + (cT-minTemperature)/10*intervalDistanse*2)//当前温度解析为填空柱高

                //柱边框
                ctx.beginPath();
                ctx.lineWidth=3;
                ctx.strokeStyle='#fff';
//                ctx.fillStyle="#f00";
//                ctx.moveTo(allw,allw)
                ctx.moveTo(allw+r*Math.sin(120-beginAng),allH+r*Math.cos(120-beginAng));
                ctx.lineTo(allw+r*Math.sin(120-beginAng),100);
                ctx.lineTo(allw-r*Math.sin(120-beginAng),100);
                ctx.lineTo(allw-r*Math.sin(120-beginAng),allH+r*Math.cos(120-beginAng));
                ctx.stroke();
//                ctx.fill();
                ctx.font = "18px bold 黑体";
                // 设置颜色
                ctx.fillStyle = "#ff0";
                // 设置水平对齐方式
                ctx.textAlign = "center";
                // 设置垂直对齐方式
                ctx.textBaseline = "middle";
                // 绘制文字(参数:要写的字,x坐标,y坐标)
                ctx.fillText("℃", allw+r*Math.sin(120-beginAng)+20,100-20);
                ctx.fillText("℉", allw-r*Math.sin(120-beginAng)-20,100-20);


                ctx.closePath()

                //右刻度
                for(var i=0;i<=intervalGrid;i++){
                    ctx.beginPath();
                    ctx.lineWidth=2;
                    if(i%2==0){
                        ctx.strokeStyle='#fff';
                        ctx.moveTo(allw+r*Math.sin(120-beginAng),allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse)
                        ctx.lineTo(allw+r*Math.sin(120-beginAng)+20,allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse)
                        ctx.font = "18px bold 黑体";
                        // 设置颜色
                        ctx.fillStyle = "#ff0";
                        // 设置水平对齐方式
                        ctx.textAlign = "center";
                        // 设置垂直对齐方式
                        ctx.textBaseline = "middle";
                        // 绘制文字(参数:要写的字,x坐标,y坐标)
                        ctx.fillText(minTemperature+10*i/2, allw+r*Math.sin(120-beginAng)+45,allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse);
                    }else{
                        ctx.strokeStyle='#fee';
                        ctx.moveTo(allw+r*Math.sin(120-beginAng),allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse)
                        ctx.lineTo(allw+r*Math.sin(120-beginAng)+10,allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse)
                    }
                    ctx.stroke()
                    ctx.closePath()
                }

                //左刻度
                for(var i=0;i<=intervalGrid;i++){
                    ctx.beginPath();
                    ctx.lineWidth=2;
                    if(i%2==0){
                        ctx.strokeStyle='#fff';
                        ctx.moveTo(allw-r*Math.sin(120-beginAng),allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse)
                        ctx.lineTo(allw-r*Math.sin(120-beginAng)-20,allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse)
                        ctx.font = "18px bold 黑体";
                        // 设置颜色
                        ctx.fillStyle = "#ff0";
                        // 设置水平对齐方式
                        ctx.textAlign = "center";
                        // 设置垂直对齐方式
                        ctx.textBaseline = "middle";
                        // 绘制文字(参数:要写的字,x坐标,y坐标)
                        ctx.fillText(32+(minTemperature+10*i/2)*1.8, allw-r*Math.sin(120-beginAng)-45,allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse);
                    }else{
                        ctx.strokeStyle='#fee';
                        ctx.moveTo(allw-r*Math.sin(120-beginAng),allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse)
                        ctx.lineTo(allw-r*Math.sin(120-beginAng)-10,allH+r*Math.cos(120-beginAng)-10-i*intervalDistanse)
                    }
                    ctx.stroke()
                    ctx.closePath()
                }




                //柱填充
                ctx.beginPath();

                ctx.lineWidth=3;
                ctx.strokeStyle='#fff';
                ctx.fillStyle="#f00";
//                ctx.moveTo(allw,allw)
                ctx.moveTo(allw+r*Math.sin(120-beginAng),allH+r*Math.cos(120-beginAng));
                ctx.lineTo(allw+r*Math.sin(120-beginAng),currentTemperature);
                ctx.lineTo(allw-r*Math.sin(120-beginAng),currentTemperature);
                ctx.lineTo(allw-r*Math.sin(120-beginAng),allH+r*Math.cos(120-beginAng));
                ctx.stroke();
                ctx.fill();

                ctx.closePath()

                //圆填充
                ctx.beginPath();
                ctx.lineWidth=3;
                ctx.strokeStyle='#fff';
                ctx.fillStyle="#f00";
                ctx.arc(allw,allH,r,beginAng,endAng,true);
                ctx.stroke();
                ctx.fill();
                ctx.closePath()
            }
        }
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值