我在场景中有2个相同的Item
个。每个都可以拖动,也可以接受与其他拖动对象的交互。为了避免与自身的交互,我在适当的拖动函数中设置drop.accept = false
。当我拖动rect1
而不是rect2
时,它可以正常工作。 rect2
响应进入和退出事件。但是,如果之后我尝试将rect2
移到rect1
上,则拖动机制停止工作并且根本不会调用拖动事件,即使我再次移动rect1
超过rect2
。
我的目标是在每次拖动时知道拖动的对象以及移动的对象。
import QtQuick 2.4
import QtQuick.Window 2.0
Window {
id: win
width: 800
height: 600
Rectangle {
id: rect1
objectName: "rect1"
width: 100
height: 100
x: 200
y: 200
color: "orange"
property bool hold: false
z: hold ? 2 : 1
Text {
anchors.centerIn: parent
text: "rect1"
}
MouseArea {
id: area1
anchors.fill: parent
drag.target: rect1.hold ? rect1 : undefined
onPressed: rect1.hold = true;
onReleased: rect1.hold = false;
}
DropArea {
anchors.fill: parent
onEntered: {
if(drag.source == rect1) {
drag.accepted = false;
}
else{
drag.accepted = true;
console.log("rect1: entered");
}
}
onExited: {
if(drag.source == rect1) {
drag.accepted = false;
}
else {
drag.accepted = true;
console.log("rect1: exited");
}
}
}
Drag.active: rect1.hold
Drag.source: rect1.hold ? rect1 : undefined
Drag.hotSpot: Qt.point(width/2,height/2)
}
Rectangle {
id: rect2
objectName: "rect2"
width: 100
height: 100
x: 500
y: 300
color: "lightblue"
property bool hold: false
z: hold ? 2 : 1
Text {
anchors.centerIn: parent
text: "rect2"
}
MouseArea {
id: area2
anchors.fill: parent
drag.target: rect2.hold ? rect2 : undefined
onPressed: rect2.hold = true;
onReleased: rect2.hold = false;
}
DropArea {
anchors.fill: parent
onEntered: {
if(drag.source == rect2) {
drag.accepted = false;
}
else {
drag.accepted = true;
console.log("rect2: entered");
}
}
onExited: {
if(drag.source == rect2) {
drag.accepted = false;
}
else {
drag.accepted = true;
console.log("rect2: exited");
}
}
}
Drag.active: rect2.hold
Drag.source: rect2.hold ? rect2 : undefined
Drag.hotSpot: Qt.point(width/2,height/2)
}
}