http://qt-project.org/wiki/Drag_and_Drop_within_a_GridView에 주어진 접근 방식을 따르고 있습니다.QML : 이동 가능한 셀이있는 GridView (끌어서 놓기)
제시된 프로그램이 있습니다. 드래그 앤 드롭이 작동하지만 드롭 후 나는 내 그리드에서 빈 공간을 얻습니다. 어떻게 개선 할 수 있습니까?
또한, 46 번째 줄의 indexAt() 함수가 드래그 작업 중에 올바르게 작동하지 않는 것으로 나타났습니다. 따라서 gridArea.index에 대한 명시적인 계산을 추가해야했습니다.
(EDITED) 엘리먼트를 삭제 한 후에 원래 위치 (표시된 엘리먼트 아래)에 반환되기 때문에 빈 공간이 나타납니다. GridView가 업데이트되지 않고 불규칙한 상태로 완료됩니다.
import QtQuick 1.1
GridView {
id: mainGrid
cellWidth: 165; cellHeight: 95
width: 5*cellWidth; height: 4*cellHeight
model: myModel
delegate: myButton
ListModel {
id: myModel
function createModel() {
for (var i=1; i<=20; i++) {
myModel.append({"display": i, "uid": i})
}
}
Component.onCompleted: {createModel()}
}
Component {
id: myButton
Rectangle {
id: item
width: mainGrid.cellWidth-5; height: mainGrid.cellHeight-5;
border.width: 1
property int uid: (index >= 0) ? myModel.get(index).uid : -1
Text {
anchors.centerIn: parent
text: display
font.pixelSize: 48
}
states: [
State {
name: "active"; when: gridArea.activeId == item.uid
PropertyChanges {target: item; x: gridArea.mouseX-80; y: gridArea.mouseY-45; z: 10; smooth: false}
}
]
}
}
MouseArea {
id: gridArea
anchors.fill: parent
hoverEnabled: true
preventStealing : true
//property int index: mainGrid.indexAt(mouseX, mouseY) //WHY IS THIS NOT WORKING RELIABLE?
property int mX: (mouseX < 0) ? 0 : ((mouseX < mainGrid.width - mainGrid.cellWidth) ? mouseX : mainGrid.width - mainGrid.cellWidth)
property int mY: (mouseY < 0) ? 0 : ((mouseY < mainGrid.height - mainGrid.cellHeight) ? mouseY : mainGrid.height - mainGrid.cellHeight)
property int index: parseInt(mX/mainGrid.cellWidth) + 5*parseInt(mY/mainGrid.cellHeight) //item underneath cursor
property int activeId: -1 //uid of active item
property int activeIndex //current position of active item
onPressAndHold: {
activeId = mainGrid.model.get(activeIndex=index).uid
}
onReleased: {
activeId = -1
}
onPositionChanged: {
if (activeId != -1 && index != -1 && index != activeIndex) {
mainGrid.model.move(activeIndex, activeIndex = index, 1)
}
}
}
}