QML:如何拒绝放置操作

QML: How to reject drop action

我有一个 DropArea 和两个元素。我想要 DropArea 如果 DropArea 已经有一个元素被丢弃,另一个元素不允许放入,除非第一个移出。

DropArea {
    property bool dropped: false

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

但看起来 drop.accepted 不起作用, 顺便说一句,无论如何要获得对象被丢弃在 DropArea

改用drop.accept()。以上可按如下方式进行:

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

        containsItem = true;
    }
}

也不要使用 dropped 属性,因为它已经是 onDropped 事件处理程序中的附加 属性。

编辑: 如果 rect 是要拖放的 Item 那么:

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!");
                    }
    }
}

您应该控制是否必须在 onReleased 中删除该项目,检查 dropped 属性。

完整示例:

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;
            }
        }
    }
}