QML: How to reject drop action

2019-02-25 13:24发布

I have one DropArea and two elements. I want DropArea reject the drop event if the DropArea already got one element be dropped, the another element not allow drop into, unless the first one move out.

DropArea {
    property bool dropped: false

    onDropped: {
        drop.accepted = !dropped;
        dropped = true;
    }
    onExited: dropped = false
}

But looks like drop.accepted not work, BTW anyway to get the objects was dropped in DropArea

标签: qt qml qtquick2
2条回答
一夜七次
2楼-- · 2019-02-25 13:38

Use drop.accept() instead. The above can be done as follows:

property bool containsItem: false
DropArea {
    id: dropArea
    anchors.fill: parent
    onDropped: {
        if(containsItem)
            drop.accept(Qt.IgnoreAction)
        else
            drop.accept()

        containsItem = true;
    }
}

Also donot use dropped property as it is already an attached property inside onDropped event handler.

Edit: If rect is the Item to be dragged and dropped then:

Rectangle {
    id: rect
    width: 40; height: 40
    color: "red"

    Drag.active: dragArea.drag.active
    Drag.hotSpot.x: 20
    Drag.hotSpot.y: 20

    MouseArea {
        id: dragArea
        anchors.fill: parent
        drag.target: parent
        onReleased: if (rect.Drag.drop() !== Qt.IgnoreAction) {
                        console.log("Accepted!");
                    } else {
                        console.log("Rejected!");
                    }
    }
}
查看更多
姐就是有狂的资本
3楼-- · 2019-02-25 13:57

You should control if the item must be dropped or not in onReleased, checking the dropped property.

Full example:

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    id: win
    visible: true
    width: 800
    height: 600
    title: qsTr("Hello World")

    Repeater {
        model: 10
        Rectangle {
            id: rect
            width: 50
            height: 50
            z: mouseArea.drag.active ||  mouseArea.pressed ? 2 : 1
            color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
            x: Math.random() * (win.width / 2 - 100)
            y: Math.random() * (win.height - 100)
            property point beginDrag
            property bool caught: false
            border { width:2; color: "white" }
            radius: 5
            Drag.active: mouseArea.drag.active

            Text {
                anchors.centerIn: parent
                text: index
                color: "white"
            }
            MouseArea {
                id: mouseArea
                anchors.fill: parent
                drag.target: parent
                onPressed: {
                    rect.beginDrag = Qt.point(rect.x, rect.y);
                }
                onReleased: {
                    if(!rect.caught || dragTarget.dropped) {
                        backAnimX.from = rect.x;
                        backAnimX.to = beginDrag.x;
                        backAnimY.from = rect.y;
                        backAnimY.to = beginDrag.y;
                        backAnim.start()
                    }

                    parent.Drag.drop()

                    console.log("MouseArea - containsDrag " + dragTarget.dropped)
                }

            }
            ParallelAnimation {
                id: backAnim
                SpringAnimation { id: backAnimX; target: rect; property: "x";
                                  duration: 500; spring: 2; damping: 0.2 }
                SpringAnimation { id: backAnimY; target: rect; property: "y";
                                  duration: 500; spring: 2; damping: 0.2 }
            }
        }
    }

    Rectangle {
        anchors {
            top: parent.top
            right:  parent.right
            bottom:  parent.bottom
        }
        width: parent.width / 2
        color: "gold"
        DropArea {
            id: dragTarget
            anchors.fill: parent

            property bool dropped: false

            onEntered: {
                console.log("onEntered " + containsDrag)
                drag.source.caught = true;
            }
            onExited: {
                console.log("onExited " + containsDrag)
                dropped = false;
            }
            onDropped:
            {
                console.log("onDropped");
                dropped = true;
            }
        }
    }
}
查看更多
登录 后发表回答