qt - 拖放项目时滚动项目

标签 qt drag-and-drop qml drag qtquick2

关注此Qt tutorial我写了这个简单的代码。有一个水平 ListView ,上面有一些简单的彩色矩形作为模型项的委托(delegate)。

import QtQuick 2.5
import QtQuick.Window 2.0
import QtQml.Models 2.2

Window {
    visible: true
    width: 300
    height: 120
    title: qsTr("Hello World")

    Rectangle {
        anchors.fill: parent;

        ListView{
            id: timeline
            anchors.fill: parent
            orientation: ListView.Horizontal
            model: visualModel
            delegate: timelineDelegate

            moveDisplaced: Transition {
                NumberAnimation{
                    properties: "x,y"
                    duration: 200
                }
            }

            DelegateModel {
                id: visualModel
                model: timelineModel
                delegate: timelineDelegate
            }

            Component {
                id: timelineDelegate


                MouseArea {
                    id: dragArea

                    width: 100; height: 100

                    property bool held: false

                    drag.target: held ? content : undefined
                    drag.axis: Drag.XAxis

                    onPressAndHold: held = true
                    onReleased: held = false

                    Rectangle {
                        id: content

                        anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter }
                        width: 100
                        height: 100

                        color: colore
                        opacity: dragArea.held ? 0.8 : 1.0

                        Drag.active: dragArea.held
                        Drag.source: dragArea
                        Drag.hotSpot.x: width / 2
                        Drag.hotSpot.y: height / 2

                        states: State{
                            when: dragArea.held
                            ParentChange { target: content; parent: timeline }
                            AnchorChanges {
                                target: content
                                anchors { horizontalCenter: undefined; verticalCenter: undefined }
                            }
                        }
                    }

                    DropArea {
                        anchors.fill: parent
                        onEntered: {
                            visualModel.items.move( drag.source.DelegateModel.itemsIndex, dragArea.DelegateModel.itemsIndex)
                            timeline.currentIndex = dragArea.DelegateModel.itemsIndex
                        }
                    }
                }
            }

            ListModel {
                id: timelineModel
                // @disable-check M16
                ListElement { colore: "blue" }
                // @disable-check M16
                ListElement { colore: "orange" }
                // @disable-check M16
                ListElement { colore: "red" }
                // @disable-check M16
                ListElement { colore: "yellow" }
                // @disable-check M16
                ListElement { colore: "green" }
                // @disable-check M16
                ListElement { colore: "yellow" }
                // @disable-check M16
                ListElement { colore: "red" }
                // @disable-check M16
                ListElement { colore: "blue" }
                // @disable-check M16
                ListElement { colore: "green" }
            }
        }
    }
}

如果我按住一个Item,我可以将它与另一个元素交换,从而产生漂亮的移动效果。
当列表中有很多项目并且目标位置超出可见项目时,问题就开始了。我可以拖动项目广告将其移动到右边框或左边框附近...这里移动效果绝对不好。

当项目到达边界附近时,是否有正确滚动列表的最佳实践?
我希望在项目接触边框之前开始滚动!

不错的

The nice one

坏的

The bad one

最佳答案

ListViewListView.currentIndex 时滚动被改变了。即拖放区域的最后一行:

timeline.currentIndex = dragArea.DelegateModel.itemsIndex

表示当前索引(始终可见)是拖动的项目。然而,如果拖动的项目到达边界,用户期望不仅看到拖动的项目,而且看到它旁边的项目。因此,您需要向 currentIndex 添加一项:

timeline.currentIndex = dragArea.DelegateModel.itemsIndex + 1

现在,如果将项目拖动到右边框, ListView 会正确滚动到右侧。为了使其在左边框和右边框中都可用,我们需要添加一些数学知识:

MouseArea {
    id: dragArea

    property int lastX: 0
    property bool moveRight: false

    onXChanged: {
        moveRight = lastX < x;
        lastX = x;
    }

    //....

    DropArea {
        anchors.fill: parent
        onEntered: {
            visualModel.items.move( drag.source.DelegateModel.itemsIndex, 
                                    dragArea.DelegateModel.itemsIndex)
            if (dragArea.moveRight)
                timeline.currentIndex = dragArea.DelegateModel.itemsIndex + 1
            else
                timeline.currentIndex = dragArea.DelegateModel.itemsIndex - 1
        }
    }
}

关于qt - 拖放项目时滚动项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40789412/

相关文章:

c++ - Qt 5.3 : Accessing/returning/calling checkboxes that are created dynamically(? )

c++ - 为什么我无法获得有关用户使用的证书的正确信息?

c++ - QWidget 如何在子部件中接收 keyPressEvent

javascript - 在网格中垂直拖放单元格

javascript - 拖放、裁剪和调整图像大小

c++ - QML 在 C++ 加载器中动态插入组件

c++ - Qt 启动画面不显示

c# - 拖放自定义对象

python - 更新只能从 GUI 线程或从 QQuickItem::updatePaintNode() 安排

android - 在android上加载外部qml文件