qt - DropArea 不通知有关 onEntered、onExited、onDropped 的操作

标签 qt drag-and-drop qml qt5 qtquick2

我有 Rectangle 填充了 MouseArea,它在 onPressAndHold() 处理程序上显示第二个 Rectangle 并传输 操作拖到那个Rectangle。问题是,当我将第二个 Rectangle 移到 DropArea 上时,它不会通知任何操作(onEnteredonExitedonDropped)。我尝试以多种组合来执行此操作,但从未奏效。这是一个示例,我是否遗漏了什么?

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 Creator 的“欢迎”屏幕上点击“示例”按钮并搜索“拖放 qml”。

关于qt - DropArea 不通知有关 onEntered、onExited、onDropped 的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34844314/

相关文章:

c++ - 在 Mac 上使用 Qt 和介子时出现问题

Angular Nested Drag and Drop/CDK Material cdkDropListGroup cdkDropList 嵌套

c++ - Qt VirtualKeyboard 启用和禁用键

qt - 无法处理 QML 中的子矩形鼠标事件

c++ - 在 Windows 上安装 MongoDB C++ 驱动程序。

c++ - 如何在Qt中绘制一个 'x'?

windows - 如何将 OpenSSL 与基于 Qt WebKit 的应用程序一起分发到 Windows?

delphi - 从一种表单拖放到另一种表单中的任意位置

jQuery UI - 如何知道拖动的元素是否位于元素 #x 的顶部?

c++ - 在 QML 场景中显示内存中的网格