我有一个DropArea和两个元素。如果DropArea已经删除了一个元素,我希望DropArea拒绝drop事件,除非第一个元素移出,否则不允许其他元素放入。

DropArea {
    property bool dropped: false

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


但是看起来drop.accepted不起作用,
无论如何要获取对象的BTW放在DropArea

最佳答案

您应检查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;
            }
        }
    }
}

关于qt - QML:如何拒绝放置 Action ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38068528/

10-09 06:33