QML:如何拒绝掉落动作

时间:2016-06-28 06:25:11

标签: qt qml qtquick2

我有一个DropArea和两个元素。我希望DropArea拒绝drop事件,如果DropArea已经删除了一个元素,另一个元素不允许丢弃,除非第一个元素移出。

DropArea {
    property bool dropped: false

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

但看起来drop.accepted不起作用, BTW无论如何都要在DropArea

中删除对象

2 个答案:

答案 0 :(得分:2)

请改用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是要拖放的项目,那么:

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

答案 1 :(得分:2)

您应该在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;
            }
        }
    }
}