QML Canvas画箭头

本文介绍了一个使用Qt Quick进行图形界面开发的例子,展示了如何通过鼠标操作调整矩形选框的大小和位置,并在两个矩形之间绘制箭头指示关联。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import QtQml 2.14
import QtQuick 2.15

Rectangle{
    id:root
    color:"red"
    property bool isSelFirstRect: true
    Image {
        id: background
        anchors{
            fill:parent
        }
        source: "qrc:/image/background.jpeg"
    }

    onIsSelFirstRectChanged: {
        canvas.requestPaint()
    }

    Rectangle{
        id:firstRect
        x:100
        y:100
        width: 200
        height: 200
        color: "transparent"
        border.width: 2
        border.color: root.isSelFirstRect?"green":"#FFFFFF"
        onWidthChanged: {
            canvas.requestPaint()
        }
        onHeightChanged: {
            canvas.requestPaint()
        }
        onXChanged: {
            canvas.requestPaint()

        }
        onYChanged: {
            canvas.requestPaint()
        }
        //左上
        Rectangle{
            visible:  root.isSelFirstRect
            width: 20
            height: 6
            anchors{
                left: parent.left
                top: parent.top
            }
            color: "green"
        }
        Rectangle{
            visible: root.isSelFirstRect
            width: 6
            height: 20
            anchors{
                left: parent.left
                top: parent.top
            }
            color: "green"
        }
        Rectangle{
            visible:  root.isSelFirstRect
            width: 6
            height: 6
            anchors{
                left: parent.left
                top: parent.top
            }
            color: "green"
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                cursorShape:Qt.SizeFDiagCursor
                property var  clickPt: Qt.point(0,0)
                property bool  isPressed: false
                onPressed: {
                    clickPt=Qt.point(mouseX,mouseY)
                    isPressed=true
                }

                onMouseXChanged: {
                    var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
                    if(isPressed&&firstRect.width-dealt.x>40&&firstRect.height-dealt.y>40){

                        // clickPt=Qt.point(mouseX,mouseY)
                        firstRect.x=firstRect.x+dealt.x
                        firstRect.y=firstRect.y+dealt.y

                        firstRect.width-=dealt.x
                        firstRect.height-=dealt.y


                    }
                }


                onReleased: {
                    isPressed=false

                }
            }
            z:3
        }
        //右下
        Rectangle{
            visible:  root.isSelFirstRect
            width: 20
            height: 6
            anchors{
                right: parent.right
                bottom:  parent.bottom
            }
            color: "green"
        }
        Rectangle{
            visible:  root.isSelFirstRect
            width: 6
            height: 20
            anchors{
                right: parent.right
                bottom:  parent.bottom
            }
            color: "green"
        }
        Rectangle{
            visible:  root.isSelFirstRect
            width: 6
            height: 6
            anchors{
                right: parent.right
                bottom:  parent.bottom
            }
            color: "green"
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                cursorShape:Qt.SizeFDiagCursor
                property var  clickPt: Qt.point(0,0)
                property bool  isPressed: false
                onPressed: {
                    clickPt=Qt.point(mouseX,mouseY)
                    isPressed=true
                }

                onMouseXChanged: {
                    var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
                    if(isPressed&&firstRect.width+dealt.x>40&&firstRect.height+dealt.y>40){



                        firstRect.width+=dealt.x
                        firstRect.height+=dealt.y


                    }
                }


                onReleased: {
                    isPressed=false

                }
            }
            z:3
        }

        //右上
        Rectangle{
            visible:  root.isSelFirstRect
            width: 20
            height: 6
            anchors{
                right: parent.right
                top:  parent.top
            }
            color: "green"
        }
        Rectangle{
            visible: root.isSelFirstRect
            width: 6
            height: 20
            anchors{
                right: parent.right
                top:  parent.top
            }
            color: "green"
        }
        Rectangle{
            visible: root.isSelFirstRect
            width: 6
            height: 6
            anchors{
                right: parent.right
                top:  parent.top
            }
            color: "green"
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                cursorShape:Qt.SizeBDiagCursor
                property var  clickPt: Qt.point(0,0)
                property bool  isPressed: false
                onPressed: {
                    clickPt=Qt.point(mouseX,mouseY)
                    isPressed=true
                }

                onPositionChanged: {
                    var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
                    if(isPressed&&firstRect.width+dealt.x>40&&firstRect.height-dealt.y>40){

                        //  clickPt=Qt.point(mouseX,mouseY)
                        firstRect.y=firstRect.y+dealt.y
                        firstRect.width+=dealt.x
                        firstRect.height-=dealt.y


                    }
                }


                onReleased: {
                    isPressed=false

                }

            }
            z:3
        }

        //左下
        Rectangle{
            visible: root.isSelFirstRect
            width: 20
            height: 6
            anchors{
                left: parent.left
                bottom:  parent.bottom
            }
            color: "green"
        }
        Rectangle{
            visible: root.isSelFirstRect
            width: 6
            height: 20
            anchors{
                left: parent.left
                bottom:  parent.bottom
            }
            color: "green"
        }
        Rectangle{
            visible: root.isSelFirstRect
            width: 6
            height: 6
            anchors{
                left: parent.left
                bottom:  parent.bottom
            }
            color: "green"
            MouseArea{
                anchors.fill: parent

                enabled: true
                hoverEnabled: true
                cursorShape:Qt.SizeBDiagCursor
                property var  clickPt: Qt.point(0,0)
                property bool  isPressed: false
                onPressed: {
                    clickPt=Qt.point(mouseX,mouseY)
                    isPressed=true
                }

                onMouseXChanged: {
                    var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
                    if(isPressed&&firstRect.width-dealt.x>40&&firstRect.height+dealt.y>40){


                        firstRect.x=firstRect.x+dealt.x
                        firstRect.width-=dealt.x
                        firstRect.height+=dealt.y


                    }
                }


                onReleased: {
                    isPressed=false

                }
            }
            z:3
        }
        Rectangle{
            visible: root.isSelFirstRect
            width: 20
            height: 2
            color: "green"
            anchors.centerIn: parent
        }

        Rectangle{

            visible: root.isSelFirstRect
            width: 2
            height: 20
            color: "green"
            anchors.centerIn: parent
        }
        MouseArea{
            anchors.fill: parent
            //  hoverEnabled: true

            onPressed: {
                if(mouse.button==Qt.LeftButton){
                    root.isSelFirstRect=true

                }
            }

            drag.target: firstRect
            drag.axis: Drag.XAndYAxis

        }


    }



    Rectangle{
        id:secondRect
        x:500
        y:500
        width: 200
        height: 200
        color: "transparent"
        border.width: 2
        border.color: !root.isSelFirstRect?"green":"#FFFFFF"
        onWidthChanged: {
            canvas.requestPaint()
        }
        onHeightChanged: {
            canvas.requestPaint()
        }
        onXChanged: {
            canvas.requestPaint()

        }
        onYChanged: {
            canvas.requestPaint()
        }
        //左上
        Rectangle{
            visible:  !root.isSelFirstRect
            width: 20
            height: 6
            anchors{
                left: parent.left
                top: parent.top
            }
            color: "green"
        }
        Rectangle{
            visible: !root.isSelFirstRect
            width: 6
            height: 20
            anchors{
                left: parent.left
                top: parent.top
            }
            color: "green"
        }
        Rectangle{
            visible:  !root.isSelFirstRect
            width: 6
            height: 6
            anchors{
                left: parent.left
                top: parent.top
            }
            color: "green"
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                cursorShape:Qt.SizeFDiagCursor
                property var  clickPt: Qt.point(0,0)
                property bool  isPressed: false
                onPressed: {
                    clickPt=Qt.point(mouseX,mouseY)
                    isPressed=true
                }

                onMouseXChanged: {
                    var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)

                    if(isPressed&&secondRect.width-dealt.x>40&&secondRect.height-dealt.y>40){
                        secondRect.x=secondRect.x+dealt.x
                        secondRect.y=secondRect.y+dealt.y
                        secondRect.width-=dealt.x
                        secondRect.height-=dealt.y


                    }
                }


                onReleased: {
                    isPressed=false

                }
            }
            z:3
        }
        //右下
        Rectangle{
            visible:  !root.isSelFirstRect
            width: 20
            height: 6
            anchors{
                right: parent.right
                bottom:  parent.bottom
            }
            color: "green"
        }
        Rectangle{
            visible:  !root.isSelFirstRect
            width: 6
            height: 20
            anchors{
                right: parent.right
                bottom:  parent.bottom
            }
            color: "green"
        }
        Rectangle{
            visible:  !root.isSelFirstRect
            width: 6
            height: 6
            anchors{
                right: parent.right
                bottom:  parent.bottom
            }
            color: "green"
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                cursorShape:Qt.SizeFDiagCursor
                property var  clickPt: Qt.point(0,0)
                property bool  isPressed: false
                onPressed: {
                    clickPt=Qt.point(mouseX,mouseY)
                    isPressed=true
                }

                onMouseXChanged: {
                    var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
                    if(isPressed&&secondRect.width+dealt.x>40&&secondRect.height+dealt.y>40){

                        //   clickPt=Qt.point(mouseX,mouseY)

                        secondRect.width+=dealt.x
                        secondRect.height+=dealt.y


                    }
                }


                onReleased: {
                    isPressed=false

                }
            }
            z:3
        }

        //右上
        Rectangle{
            visible:  !root.isSelFirstRect
            width: 20
            height: 6
            anchors{
                right: parent.right
                top:  parent.top
            }
            color: "green"
        }
        Rectangle{
            visible: !root.isSelFirstRect
            width: 6
            height: 20
            anchors{
                right: parent.right
                top:  parent.top
            }
            color: "green"
        }
        Rectangle{
            visible: !root.isSelFirstRect
            width: 6
            height: 6
            anchors{
                right: parent.right
                top:  parent.top
            }
            color: "green"
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                cursorShape:Qt.SizeBDiagCursor
                property var  clickPt: Qt.point(0,0)
                property bool  isPressed: false
                onPressed: {
                    clickPt=Qt.point(mouseX,mouseY)
                    isPressed=true
                }

                onMouseXChanged: {
                    var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
                    if(isPressed&&secondRect.width+dealt.x>40&&secondRect.height-dealt.y>40){

                        // clickPt=Qt.point(mouseX,mouseY)
                        secondRect.y=secondRect.y+dealt.y
                        secondRect.width+=dealt.x
                        secondRect.height-=dealt.y


                    }
                }


                onReleased: {
                    isPressed=false

                }

            }
            z:3
        }

        //左下
        Rectangle{
            visible: !root.isSelFirstRect
            width: 20
            height: 6
            anchors{
                left: parent.left
                bottom:  parent.bottom
            }
            color: "green"
        }
        Rectangle{
            visible: !root.isSelFirstRect
            width: 6
            height: 20
            anchors{
                left: parent.left
                bottom:  parent.bottom
            }
            color: "green"
        }
        Rectangle{
            visible: !root.isSelFirstRect
            width: 6
            height: 6
            anchors{
                left: parent.left
                bottom:  parent.bottom
            }
            color: "green"
            MouseArea{
                anchors.fill: parent

                enabled: true
                hoverEnabled: true
                cursorShape:Qt.SizeBDiagCursor
                property var  clickPt: Qt.point(0,0)
                property bool  isPressed: false
                onPressed: {
                    clickPt=Qt.point(mouseX,mouseY)
                    isPressed=true
                }

                onMouseXChanged: {
                    var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
                    if(isPressed&&secondRect.width-dealt.x>40&&secondRect.height+dealt.y>40){


                        secondRect.x=secondRect.x+dealt.x
                        secondRect.width-=dealt.x
                        secondRect.height+=dealt.y


                    }
                }


                onReleased: {
                    isPressed=false

                }
            }
            z:3
        }


        Rectangle{
            visible: !root.isSelFirstRect
            width: 20
            height: 2
            color: "green"
            anchors.centerIn: parent
        }

        Rectangle{
            visible: !root.isSelFirstRect
            width: 2
            height: 20
            color: "green"
            anchors.centerIn: parent
        }
        MouseArea{
            anchors.fill: parent
            onPressed: {
                root.isSelFirstRect=false
            }

            drag.target: secondRect
            drag.axis: Drag.XAndYAxis
        }
    }



    Rectangle {
        id:topRect
        anchors{
            left: parent.left
            right: parent.right
            top: parent.top
            bottom: root.isSelFirstRect?firstRect.top:secondRect.top
        }
        z:2
        opacity: 0.5
        color: "black"
    }

    Rectangle {
        id:bottomRect
        anchors{
            left: parent.left
            right: parent.right
            bottom: parent.bottom
            top: root.isSelFirstRect?firstRect.bottom:secondRect.bottom
        }
        z:2
        opacity: 0.5
        color: "black"
    }
    Rectangle {
        id:leftRect
        anchors{
            left: parent.left
            right: root.isSelFirstRect?firstRect.left:secondRect.left
            bottom: bottomRect.top
            top: topRect.bottom
        }
        z:2
        opacity: 0.5
        color: "black"
    }
    Rectangle {
        id:rightRect
        anchors{
            left:  root.isSelFirstRect?firstRect.right:secondRect.right
            right:parent.right
            bottom: bottomRect.top
            top: topRect.bottom
        }
        z:2
        opacity: 0.5
        color: "black"
    }

    Canvas{
        id:canvas
        contextType: "2d"
        antialiasing: true

        anchors{
            fill:parent
        }
        function arrow(context, fromx, fromy, tox, toy) {
            const dx = tox - fromx;
            const dy = toy - fromy;
            const headlen = Math.sqrt(dx * dx + dy * dy) * 0.05; // length of head in pixels
            const angle = Math.atan2(dy, dx);

            context.beginPath();
            context.moveTo(fromx,fromy)
            context.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6)*0.5, toy - headlen * Math.sin(angle - Math.PI / 6)*0.5)
            context.lineTo(tox,toy)
            context.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6)*0.5, toy - headlen * Math.sin(angle + Math.PI / 6)*0.5)
            context.stroke();
            context.fill()

            context.beginPath();
            context.moveTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
            context.lineTo(tox, toy );
            context.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
            context.stroke();
            context.fill()

        }
        onPaint: {
            var context=getContext("2d")
            context.clearRect(0,0,width,height)
            context.lineWidth = 1
            context.strokeStyle = "#75FA61"
            context.fillStyle = "#75FA61"
            var fromPt=root.isSelFirstRect?Qt.point(firstRect.x+firstRect.width/2,firstRect.y+firstRect.height/2):Qt.point(secondRect.x+secondRect.width/2,secondRect.y+secondRect.height/2)
            var toPt=root.isSelFirstRect?Qt.point(secondRect.x+secondRect.width/2,secondRect.y+secondRect.height/2):Qt.point(firstRect.x+firstRect.width/2,firstRect.y+firstRect.height/2)
            arrow(context,toPt.x,toPt.y,fromPt.x,fromPt.y)

        }
        z: 2

    }

}

相关文档参考文档
Canvas学习:绘制箭头
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值