如何正确实现重叠丢弃目标?

Kay*_*ute 7 qt qml

激活放置目标后,即使光标移动到位于原始放置目标上方的另一个放置目标,它仍然处于活动状态.

这是一个QML演示:尝试将文件拖放到灰色和蓝色区域.蓝色的永远不会被激活.

import QtQuick 2.1

Rectangle {
    color: "grey"
    width: 300
    height: 300

    DropArea {
        anchors.fill: parent
        onDropped: status.text = "Dropped on Grey";
    }

    Rectangle {
        color: "blue"
        anchors.fill: parent
        anchors.margins: 80

        DropArea {
            anchors.fill: parent
            # Never active!
            onDropped: status.text = "Dropped on Blue" 
        }
    }

    Text {
        x: 10
        y: 10
        id: status
        text: "Nothing dropped"
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现灰色和蓝色矩形的下降?

Blu*_*gma 3

你不能这样做,因为一旦你进入灰色区域,它就会获得焦点,并且(即使你将鼠标悬停在蓝色区域)蓝色 droparea 将永远不会收到事件。

您必须使蓝色区域成为灰色 droparea 的子区域,但现在出现了一个新问题:蓝色区域上的 onDrop,灰色区域也会获取该事件,因此如果该事件落在蓝色区域上,则必须阻止该事件(这就是 blueDrop 属性的使用):

Rectangle {
    color: "grey"
    width: 300
    height: 300

    DropArea {
        id: greyDrop;
        property bool blueDrop: false;
        anchors.fill: parent
        onDropped: blueDrop ? blueDrop = false : status.text = "Dropped on Grey";

        Rectangle {
            color: "blue"
            anchors.fill: parent
            anchors.margins: 80

            DropArea {
                anchors.fill: parent
                onDropped: { status.text = "Dropped on Blue"; greyDrop.blueDrop = true; }
            }

        }
    }
    Text {
        id: status
        text: "Nothing dropped"
    }
}
Run Code Online (Sandbox Code Playgroud)