我的目标是将item
的副本从drawer
拖动到另一个item
上。当我按住item
时,我会创建一个新的Rectangle
并将其重置为新的parent
并将其设置为drag.target
当我越过drawer
的边界时,我想close()
和drawer
。我的问题是,我正在失去对被拖动的item
的控制。即使调用了drawer.close()
,我怎么能一直拖动item
呢。下面是一个工作示例:
import QtQuick 2.4
import QtQuick.Controls 2.1
ApplicationWindow {
width: 600
height: 600
visible: true
Item{
id: root
Drawer{
id:drawer
width: 0.5 * parent.width
height: parent.height
edge: Qt.RightEdge
Rectangle {
id: item
border.width: 2
x: 100
y: 100
width: 100
height: 100
MouseArea {
id: mouseArea
anchors.fill: parent
onPressAndHold: {
var newItem = createShadow(item)
newItem.parent = root
drag.target = newItem
}
onMouseXChanged: {
//hard coding the cursor crossing the drawer boundary
var point = mapToItem(root, mouseX, mouseY)
if (point.x < 300)
drawer.close()
}
onReleased: {
drag.target.Drag.drop()
}
}
}
}
DropArea {
anchors.fill: parent
onDropped: {
//do something
}
}
}
function createShadow(itm) {
//assuming we are fetching geometry from itm
var component = Qt.createQmlObject('import QtQuick 2.4; Rectangle{x:400;y:100;width:100;height:100;opacity:0.2;color:"black"}',root, "asdf");
return component
}
}
您还应该在拖动过程中重新准备MouseArea
,以便继续接收onMouseXChanged
事件:
onPressAndHold: {
var newItem = createShadow(item)
newItem.parent = root
drag.target = newItem
parent = root
}
// ...
onReleased: {
drag.target.Drag.drop()
parent = item
}