【问题标题】:DropArea doesn't notify about actions onEntered, onExited, onDroppedDropArea 不通知有关操作 onEntered、onExited、onDropped
【发布时间】:2016-01-17 22:05:11
【问题描述】:

我的Rectangle 充满了MouseAreaonPressAndHold() 处理程序显示第二个Rectangle 并将drag 操作转移到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")
        }
    }
}

【问题讨论】:

    标签: qt drag-and-drop qml qt5 qtquick2


    【解决方案1】:

    您的代码的主要问题是您没有设置拖动的active 属性。像这样修改你的代码:

    //..........................
    Item{
        id: cloneRect
        width: 50; height:50
        visible: isRectVisible
    
        Drag.active: visible // Add this line of code
    //.....................
    

    有关更多信息,请参阅 Qt 示例。在 Qt Creator 的“欢迎”屏幕上点击“示例”按钮并搜索“拖放 qml”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 2014-07-25
      相关资源
      最近更新 更多