简体   繁体   English

拖动QML形状的坐标

[英]Drag coordinates of a QML shape

I want to draw a shape that has 4 sides with unequal length and the ability to drag its apexes. 我想绘制一个具有不等长的4个边并且可以拖动其顶点的形状。 When I dragged an apex, the sides that are connected to it should resize accordingly. 当我拖动一个顶点时,与之相连的侧面应相应调整大小。

I've found another question on SO but the proposed solution works for Rectangle s whereas I would like to develop a solution for trapezoidal shapes, like in the following picture: 在SO上发现了另一个问题,但建议的解决方案适用于Rectangle而我想开发一种梯形形状的解决方案,如下图所示:

我的照片

My current code: 我当前的代码:

property var selection: undefined
Image {
    id: image1
    anchors.fill: parent

    source: "1.jpg"

    MouseArea {
        anchors.fill: parent
        onClicked: {
            if(!selection)
                selection = selectionComponent.createObject(parent, {"x": parent.width / 4, "y": parent.height / 4, "width": parent.width / 2, "height": parent.width / 2})
        }
    }
}

Component {
    id: selectionComponent

    Rectangle {
        id: selComp
        border {
            width: 2
            color: "steelblue"
        }
        color: "#354682B4"

        property int rulersSize: 18

        MouseArea {     // drag mouse area
            anchors.fill: parent
            drag{
                target: parent
                minimumX: 0
                minimumY: 0
                maximumX: parent.parent.width - parent.width
                maximumY: parent.parent.height - parent.height
                smoothed: true
            }

            onDoubleClicked: {
                parent.destroy()        // destroy component
            }
        }


        Rectangle {
            width: rulersSize
            height: rulersSize
            radius: rulersSize
            color: "steelblue"
            anchors.left: parent.left
            anchors.top: parent.top

            MouseArea {
                anchors.fill: parent
                drag{ target: parent; axis: Drag.XAxis }
                onMouseXChanged: {
                    if(drag.active){
                        selComp.width = selComp.width - mouseX
                        selComp.height = selComp.height - mouseY
                        selComp.x = selComp.x + mouseX
                        selComp.y = selComp.y + mouseY
                        if(selComp.width < 30)
                            selComp.width = 30
                    }
                }
            }
        }

        Rectangle {
            width: rulersSize
            height: rulersSize
            radius: rulersSize
            color: "steelblue"
            anchors.left: parent.left
            anchors.bottom: parent.bottom

            MouseArea {
                anchors.fill: parent
                drag{ target: parent; axis: Drag.XAxis }
                onMouseXChanged: {
                    if(drag.active){
                        selComp.width = selComp.width - mouseX
                        selComp.height = selComp.height + mouseY
                        //selComp.x = selComp.x + mouseX
                        //selComp.y = selComp.y + mouseY
                        if(selComp.width < 30)
                            selComp.width = 30
                    }
                }
            }
        }


        Rectangle {
            width: rulersSize
            height: rulersSize
            radius: rulersSize
            color: "steelblue"
            anchors.horizontalCenter: parent.left
            anchors.verticalCenter: parent.verticalCenter

            MouseArea {
                anchors.fill: parent
                drag{ target: parent; axis: Drag.XAxis }
                onMouseXChanged: {
                    if(drag.active){
                        selComp.width = selComp.width - mouseX
                        selComp.x = selComp.x + mouseX
                        if(selComp.width < 30)
                            selComp.width = 30
                    }
                }
            }
        }



        Rectangle {
            width: rulersSize
            height: rulersSize
            radius: rulersSize
            color: "steelblue"
            anchors.horizontalCenter: parent.right
            anchors.verticalCenter: parent.verticalCenter

            MouseArea {
                anchors.fill: parent
                drag{ target: parent; axis: Drag.XAxis }
                onMouseXChanged: {
                    if(drag.active){
                        selComp.width = selComp.width + mouseX
                        if(selComp.width < 50)
                            selComp.width = 50
                    }
                }
            }
        }

        Rectangle {
            width: rulersSize
            height: rulersSize
            radius: rulersSize
            x: parent.x / 2
            y: 0
            color: "steelblue"
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.top

            MouseArea {
                anchors.fill: parent
                drag{ target: parent; axis: Drag.YAxis }
                onMouseYChanged: {
                    if(drag.active){
                        selComp.height = selComp.height - mouseY
                        selComp.y = selComp.y + mouseY
                        if(selComp.height < 50)
                            selComp.height = 50
                    }
                }
            }
        }

        Rectangle {
            width: rulersSize
            height: rulersSize
            radius: rulersSize
            x: parent.x / 2
            y: parent.y
            color: "steelblue"
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.bottom

            MouseArea {
                anchors.fill: parent
                drag{ target: parent; axis: Drag.YAxis }
                onMouseYChanged: {
                    if(drag.active){
                        selComp.height = selComp.height + mouseY
                        if(selComp.height < 50)
                            selComp.height = 50
                    }
                }
            }
        }


    }
}

This will work: 这将起作用:

Point.qml Point.qml

import QtQuick 2.0
Item {
    id: root

    signal dragged()

    Rectangle {
        anchors.centerIn: parent
        width: 20
        height: 20
        color: "blue"
        opacity: 0.3

        MouseArea {
            anchors.fill: parent
            drag.target: root
            onPositionChanged: {
                if(drag.active) {
                    dragged()
                }
            }
        }
    }
}

main.qml: main.qml:

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    visible: true

    width: 600
    height: 600

    Point {
        id: pointA
        x: 50
        y: 50
    }

    Point {
        id: pointB
        x: 250
        y: 50
    }

    Point {
        id: pointC
        x: 250
        y: 250
    }

    Point {
        id: pointD
        x: 50
        y: 250
    }


    Item {
        anchors.fill: parent

        Canvas {
            id: canvas
            anchors.fill: parent
            onPaint: {
                var ctx = canvas.getContext('2d');
                ctx.moveTo(pointA.x, pointA.y);
                ctx.lineTo(pointB.x, pointB.y);
                ctx.lineTo(pointC.x, pointC.y);
                ctx.lineTo(pointD.x, pointD.y);
                ctx.lineTo(pointA.x, pointA.y);
                ctx.stroke();
            }
            Component.onCompleted: {
                pointA.dragged.connect(repaint)
                pointB.dragged.connect(repaint)
                pointC.dragged.connect(repaint)
                pointD.dragged.connect(repaint)
            }

            function repaint() {
                var ctx = getContext("2d");
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                ctx.beginPath();
                requestPaint()
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM