我正試圖在QML中實現基本的拖放操作。在功能上,它的工作原理 - 我可以拖動一個字符串。但是,我無法讓我的可拖動Rectangle對象跟隨光標。它將矩形的x和y正確設置爲可見的框架,但是它保持靜止而不是用鼠標移動。這是我的代碼:可拖動的項目不能在QML中工作
MouseArea {
id: mouseArea
anchors.fill: parent
drag.target: draggable
}
Rectangle {
id: draggable
height: 18
width: dragText.width + 8
clip: true
color: "#ff333333"
border.width: 2
border.color: "#ffaaaaaa"
visible: false
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
Drag.active: mouseArea.drag.active
Drag.hotSpot.x: 0
Drag.hotSpot.y: 0
Drag.mimeData: { "text/plain": "Teststring" }
Drag.dragType: Drag.Automatic
Drag.onDragStarted: {
visible = true
}
Drag.onDragFinished: {
visible = false
}
Text {
id: dragText
x: 4
text: "Teststring"
font.weight: Font.Bold
color: "#ffffffff"
horizontalAlignment: Text.AlignHCenter
}
}
你打算在這裏做什麼?你是否嘗試用'visible:false'拖動一個項目? – folibis
不,我在拖動開始時立即將項目設置爲可見(請參閱Drag.onDragStarted)。 – user1765354
拖動隱形物品有什麼意義?你的目標是什麼?也許這可以用另一種方式來完成。 – folibis