我想在将项目拖出
cursorShape
时不断更改 MouseArea
。
正如您在下面的代码中看到的,当我将鼠标保持在当前
MouseArea
时,cursorShape
将更改为DragCopyCursor
。但是,进一步,如果我将鼠标移动到另一个 mouseArea 区域,鼠标将再次变为 ArrowCursor
。
import QtQuick
import QtQuick.Controls
Window {
width: 300
height: 300
visible: true
color: "#1F1F1F"
Column {
anchors.centerIn: parent
Repeater {
model: 2
delegate: Rectangle {
z: mouseArea.drag.active ? 2 : 0
color: "#aaaaaa"
width: 300
height: 50
MouseArea {
id: mouseArea
anchors.fill: parent
drag.target: rec
Binding {
when: mouseArea.drag.active
mouseArea.cursorShape: Qt.DragCopyCursor
}
Rectangle {
id: rec
color: "white"
height: parent.height / 2
width: parent.width / 2
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
Text {
anchors.centerIn: parent
text: "test " + index
color: "black"
}
states: [
State {
when: mouseArea.drag.active
AnchorChanges {
target: rec
anchors.verticalCenter: undefined
anchors.horizontalCenter: undefined
}
}
]
}
}
}
}
}
}
如果您使用
DragHandler
而不是 MouseArea
,您可以在整个拖动过程中保留 cursorShape
:
Column {
anchors.centerIn: parent
Repeater {
model: 2
delegate: Rectangle {
id: frame
z: dragHandler.active ? 2 : 0
color: "#aaaaaa"
border.color: "pink"
width: 300
height: 50
Rectangle {
id: rec
color: "white"
height: parent.height / 2
width: parent.width / 2
x: (frame.width - width) / 2
y: (frame.height - height) / 2
Text {
anchors.centerIn: parent
text: "test " + index
color: "black"
}
DragHandler {
id: dragHandler
target: rec
cursorShape: Qt.DragCopyCursor
onActiveChanged: {
if (!active) {
rec.x = (frame.width - rec.width) / 2;
rec.y = (frame.height - rec.height) / 2;
}
}
}
}
}
}
}