DropArea不会通知有关onEntered,onExited,onDropped操作的信息 [英] DropArea doesn't notify about actions onEntered, onExited, onDropped
问题描述
我用 MouseArea
填充了 Rectangle
,它在 onPressAndHold()
处理程序上显示了第二个 Rectangle
并传输了拖动
动作到该矩形
.问题是,当我将第二个 Rectangle
移到 DropArea
上时,它不会通知任何操作( onEntered
, onExited 代码>,
onDropped
).我尝试以多种组合方式执行此操作,但从未成功.这是一个例子,我错过了什么吗?
I have Rectangle
filled with MouseArea
which on onPressAndHold()
handler reveals second Rectangle
and transfers drag
action to that Rectangle
. The problem is that when I move that second Rectangle
over DropArea
it doesn't notify about any actions (onEntered
, onExited
, onDropped
). I tried to do this in many combinations but it has never worked. Here is an example, am I missing something?
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
属性.像这样修改您的代码:
The main problem with your code is that you don't set the active
property of the drag. Modify you code like this:
//..........................
Item{
id: cloneRect
width: 50; height:50
visible: isRectVisible
Drag.active: visible // Add this line of code
//.....................
有关更多信息,请参阅Qt示例.在Qt Creator的欢迎"屏幕上,单击示例"按钮,然后搜索拖放qml".
For more information please refer to Qt examples. At Qt Creator's "Welcome" screen hit "Examples" button and search for "drag and drop qml".
这篇关于DropArea不会通知有关onEntered,onExited,onDropped操作的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!