QML从抽屉拖动到另一个项目,当抽屉关闭时,我如何保持连续拖动



我的目标是将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
}

相关内容

  • 没有找到相关文章

最新更新