快速拖拽拖动时拖放项目会抖动

时间:2016-03-07 22:04:21

标签: qt drag-and-drop qml

我有可拖动项目的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 }
                        }
                    }

        }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

我发表了评论,但我想与您分享一个可能解决方案的答案。

在这种情况下,您可以启用或禁用MouseArea以避免此问题。

这个想法是禁用onPressed插槽中的MouseArea,并在动画停止时启用它,并将其称为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;
                        }
                    }
                }
...