【问题标题】:Why the "onExited" event handler is called instead of "onDropped"?为什么调用“onExited”事件处理程序而不是“onDropped”?
【发布时间】:2020-09-11 20:57:07
【问题描述】:

我正在尝试了解如何使用 QML 的 drag-and-drop 功能。出于这个原因,我修改了Drag QML Type 文档中的第一个示例,如下所示:

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 400
    height: 400
    visible: true

    Item {
        anchors.fill: parent

        DropArea {
            x: 100; y: 100; width: 100; height: 100

            onEntered: console.log("entered")
            onExited: console.log("exited")
            onPositionChanged: console.log("dragged to (" + drag.x + ", "
                                           + drag.y + ")")
            onDropped: console.log("dropped")

            Rectangle {
                anchors.fill: parent
                color: parent.containsDrag ? "green" : "yellow"
            }
        }

        Rectangle {
            x: 10; y: 10; width: 50; height: 50
            color: "red"

            Drag.active: dragArea.drag.active
            Drag.hotSpot.x: 25
            Drag.hotSpot.y: 25

            MouseArea {
                id: dragArea
                anchors.fill: parent
                drag.target: parent
            }
        }
    }
}

因此,当我将红色矩形拖到放置区域并将其放置到那里时,我希望 dropped 会登录到控制台。相反,我得到exited

qml: entered
qml: dragged to (2, 1)
qml: dragged to (3, 2)
qml: dragged to (5, 3)
qml: dragged to (5, 4)
...
qml: dragged to (44, 39)
qml: dragged to (45, 40)
qml: dragged to (46, 40)
qml: exited <- Shouldn't this be 'dropped'

类似的问题,例如:

DropArea doesn't notify about actions onEntered, onExited, onDropped

没有提供所描述问题的解决方案。

如何使这项工作按预期进行?

【问题讨论】:

  • 对我来说,QML 中的拖放系统简直令人作呕。你可能不得不玩Drag.dragType。我记得如果这不是自动的,你必须以编程方式启动和停止它。
  • 或尝试this 解决方案,应该对您有所帮助。
  • QML 中的拖放系统对我来说很恶心 我也有同感。感谢您的提示和链接!
  • @folibis,真的很恶心。事实证明,虽然我能够移动矩形,但这根本不被视为拖动。 :(谢谢你,现在我明白了。我可以请你写这个作为答案,所以我可以接受。

标签: qt qml


【解决方案1】:

问题出在下面一行:

Drag.active: dragArea.drag.active

来自有关 Drag.active 属性的文档:

将此属性设置为 true 还会发送一个 QDragEnter 事件到 具有项目当前位置的场景。将其设置为 false 将发送 一个 QDragLeave 事件。

因此,当您释放鼠标按钮时,MouseArea 会将 drag.active 属性更改为 false 并且您会收到退出信号。尝试添加到您的 MouseArea:onReleased: parent.Drag.drop()

我会说 Qt 对于 Qml 拖放有不好的例子。

【讨论】:

    猜你喜欢
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2021-07-02
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多