Qt DropArea不';不要通知已取消、已退出或已放弃的操作

Qt DropArea不';不要通知已取消、已退出或已放弃的操作,qt,drag-and-drop,qml,qt5,qtquick2,Qt,Drag And Drop,Qml,Qt5,Qtquick2,我用鼠标earea填充了矩形,在onPressAndHold()处理程序上显示第二个矩形,并将拖动操作转移到该矩形。问题是,当我将第二个矩形移动到放置区上时,它不会通知任何操作(onenterned,onExited,onDropped)。我尝试过多种组合,但从未奏效。举个例子,我是否遗漏了什么 import QtQuick 2.0 import QtQuick.Window 2.0 Window { id: appDrawerRoot visible: true wi

我用
鼠标earea
填充了
矩形
,在
onPressAndHold()
处理程序上显示第二个
矩形
,并将
拖动
操作转移到该
矩形
。问题是,当我将第二个
矩形
移动到
放置区
上时,它不会通知任何操作(
onenterned
onExited
onDropped
)。我尝试过多种组合,但从未奏效。举个例子,我是否遗漏了什么

import QtQuick 2.0
import QtQuick.Window 2.0

Window {
    id: appDrawerRoot
    visible: true
    width: 360; height: 360
    property bool isRectVisible: false

    Rectangle{
        id:rect
        color: "blue"
        x:50; y:50
        width: 50; height: 50

        MouseArea{
            anchors.fill: parent

            onPressed: {
                cloneRect.x = rect.x
                cloneRect.y = rect.y
            }
            onPressAndHold: {
                isRectVisible = true
                drag.target = cloneRect
            }
            onReleased: {
                drag.target = undefined
                isRectVisible = false
                cloneRect.x = rect.x
                cloneRect.y = rect.y +100
            }
        }
    }

    Item{
        id: cloneRect
        width: 50; height:50
        visible: isRectVisible

        MouseArea{
            id: mouseArea
            width:50; height:50
            anchors.centerIn: parent

            Rectangle{
                id:tile
                width: 50; height:50
                color:"black"
                opacity: 0.5
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                Drag.hotSpot.x: 25
                Drag.hotSpot.y: 25
            }
        }
    }

    DropArea {
        id:dropArea
        x:153
        y:158
        z:-1
        width:100; height: 100

        Rectangle{
            anchors.fill: parent
            color: "Green"
        }
        onEntered: {
            drag.source.opacity = 1
            console.log("ENTERED")
        }
        onExited: {
            drag.source.opacity = 0.5
            console.log("EXITED")
        }
        onDropped:
        {
            console.log("DROPPED")
        }
    }
}

代码的主要问题是没有设置拖动的
active
属性。按如下方式修改您的代码:

//..........................
Item{
    id: cloneRect
    width: 50; height:50
    visible: isRectVisible

    Drag.active: visible // Add this line of code
//.....................
有关更多信息,请参阅Qt示例。在Qt创建者的“欢迎”屏幕上,点击“示例”按钮并搜索“拖放qml”