我想使用ListView爲我的遊戲創建廣告資源用戶界面,其中的項目可以通過將其拖放到關卡中從庫存中刪除。如果物品沒有正確放置(仍在庫存中),則應將其放回到拖動之前的位置。使用ListView拖放來創建廣告資源UI
我已經得到了下面的代碼,但我不知道如何實現我之後的事情,即使在看完Drag and Drop example之後。
import QtQuick 2.3
Rectangle {
id: root
width: 400
height: 400
ListView {
id: listView
width: parent.width/2
height: parent.height
model: ListModel {
Component.onCompleted: {
for (var i = 0; i < 10; ++i) {
append({value: i});
}
}
}
delegate: Item {
id: delegateItem
width: listView.width
height: 50
Rectangle {
id: dragRect
width: listView.width
height: 50
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
color: "salmon"
border.color: Qt.darker(color)
Text {
anchors.centerIn: parent
text: modelData
}
MouseArea {
id: mouseArea
anchors.fill: parent
drag.target: dragRect
}
Drag.hotSpot.x: dragRect.width/2
Drag.hotSpot.y: dragRect.height/2
}
}
}
Rectangle {
width: parent.width/2
height: parent.height
anchors.right: parent.right
color: "#aaff0011"
DropArea {
id: dropArea
anchors.fill: parent
}
}
}