【发布时间】:2016-03-07 22:04:20
【问题描述】:
我有带有可拖动项目的 QML ListView。当我有时(经常)拿项目并移动鼠标时,项目会摇回初始位置然后回到实际位置等。 它发生在 Linux 和 Windows 上的 Qt 5.5.1 上。 下面是有问题的示例代码。尝试从左向右拖动项目并查看输出日志。有时它会输出很多关于进入/离开放置区域的信息。
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Layouts 1.2
Window {
visible: true
width: Screen.width
height: Screen.height
property int num:150
Row{
anchors.fill: parent
ColumnLayout{
id:col1
width: parent.width/2
height: parent.height
DropArea{
anchors.fill: parent
onEntered: {
console.log("entered:"+drag.source)
}
onExited: {
console.log("exited:"+drag.source)
}
}
ListView{
spacing: 2
model:num
anchors.fill: parent
delegate: Rectangle{
width: parent.width/2
height: width
color:"green"
}
}
}
ColumnLayout{
id:col2
width: parent.width/2
height: parent.height
ListView{
anchors.fill: parent
spacing: 2
model:num
delegate: Rectangle{
id:restItem
property point beginDrag
property int maxDragX: 96
width: parent.width/2
height: width
color:"red"
Drag.active: mouseArea.drag.active
MouseArea {
id: mouseArea
anchors.fill: parent
drag{
target: restItem
axis: Drag.XAxis
smoothed: true
threshold: width/3
maximumX: 0
minimumX: -maxDragX
}
preventStealing: true
onPressed: {
restItem.beginDrag = Qt.point(restItem.x, restItem.y);
}
onReleased: {
backAnimX.from = restItem.x;
backAnimX.to = beginDrag.x;
backAnimY.from = restItem.y;
backAnimY.to = beginDrag.y;
backAnim.start()
}
}
ParallelAnimation {
id: backAnim
alwaysRunToEnd: true
running: false
SpringAnimation { id: backAnimX; target: restItem; property: "x"; duration: 500; spring: 2; damping: 0.2 }
SpringAnimation { id: backAnimY; target: restItem; property: "y"; duration: 500; spring: 2; damping: 0.2 }
}
}
}
}
}
}
【问题讨论】:
-
我正在使用您的代码,并且只有在该项目仍在移动时单击并尝试移动该项目时,我才会看到此行为。如果该项目完成其移动或动画,那么我没有观察到您解释的问题。
标签: qt drag-and-drop qml