首页 > 解决方案 > Drag and Drop mechanism in qml

问题描述

I have a row consists of 10 rectangles, each rectangle can be dragged to another area, this what I have made, but I need to drag a part of the rectangles, not all of them example: the user can drag 5 rectangle at once, or he can drag 3 rectangles at once or dragging 7 rectangles at one.

here is my code:

Row {
    id: rowLayout

    property int index: 9

    width: bottomRectangle.width * 0.5
    height: bottomRectangle.height * 0.5
    anchors.centerIn: parent
    Repeater {
        id: repeater
        model: 10
        Rectangle {
            id: unitItem
            width: rowLayout.width * 0.1
            height: rowLayout.height * 0.5
            color: "#80F16F6F"
            border.width: 5
            border.color: "#FFF16F6F"
            radius: 10
            Drag.active: dragArea.drag.active
            Drag.hotSpot.x: parent.width / 2
            Drag.hotSpot.y: parent.height / 2

            MouseArea {
                id: dragArea
                anchors.fill: parent
                drag.target: unitItem

                onReleased: {
                    parent = unitItem.Drag.target !== null ? unitItem.Drag.target : dropArea
                    unitItem.Drag.drop()
                }
            }

            states: [
                State {
                    when: dragArea.drag.active
                    ParentChange { target: unitItem; parent: dropArea }
                    AnchorChanges { target: unitItem; anchors.verticalCenter: undefined; anchors.horizontalCenter: undefined }
                }
            ]
        }
    }
}

at this point, each rectangle can be dragged at its own. But I need to drag 6 rectangles for example all at once. any ideas for that?

标签: qtdrag-and-dropqml

解决方案


推荐阅读