qt拖动时拖动快速拖放项目抖动
问题描述:
我已经有QML ListView拖动项目。当我拿东西并移动鼠标时(很多时候)项目回到初始位置,然后回到实际位置等。 在Linux和Windows上发生了Qt 5.5.1。 下面是有问题的示例代码。尝试从左到右拖动项目并查看输出日志。有时它的大量输入/离开放置区域。qt拖动时拖动快速拖放项目抖动
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 }
}
}
}
}
}
}
答
我发表了评论,但我想与您分享也有可能的解决方案的答案。
在这种情况下,您可以启用或禁用MouseArea
以避免该问题。
想法是在onPressed
插槽中禁用鼠标区域,并在动画停止时启用它,并将其命名为onStopped
插槽。
...
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: {
mouseArea.enabled = false;
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 }
onStopped: {
mouseArea.enabled = true;
}
}
}
...
+1
谢谢你的回答,这是有效的。 – swex
我正在处理您的代码,并且只有在单击并尝试移动该项目时仍然移动的情况下才会看到此行为。如果该项目完成其移动或动画,那么我不会观察您解释的问题。 – Tarod