通过在移动光标时按住鼠标按钮来禁用视图的拖动

Disable draging of the view by pressing and holding a mouse button while moving the cursor

我想通过在移动光标时按住鼠标按钮来禁用列表视图的拖动。我正在尝试使用此命令实现其他一些功能,例如多项选择。我只想启用滚动。 flickableinteractive 属性 完全禁用视图的移动。有解决办法吗?

我想有一些方法可以满足您的需求,但以下解决方案有效。

想法是有一个 MouseArea 并在信号 onPressedonClicked 时将 interactive 属性 设置为 false发射。 interactive 应在 onReleased 处理程序中再次设置为 true

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ListModel {
        id: listModel
        ListElement {
            name: "ttt"
            number: "111"
        }
        ListElement {
            name: "rrr"
            number: "222"
        }
        ListElement {
            name: "sss"
            number: "333"
        }
        ListElement {
            name: "xxx"
            number: "444"
        }
        ListElement {
            name: "yyy"
            number: "555"
        }
        ListElement {
            name: "zzz"
            number: "666"
        }
        ListElement {
            name: "aaa"
            number: "777"
        }
        ListElement {
            name: "bbb"
            number: "888"
        }
        ListElement {
            name: "ccc"
            number: "999"
        }
        ListElement {
            name: "ddd"
            number: "011"
        }
        ListElement {
            name: "eee"
            number: "022"
        }
        ListElement {
            name: "fff"
            number: "033"
        }
    }

    ListView {
        id: myList
        width: 180; height: 100
        clip: true

        Component {
            id: contactsDelegate
            Rectangle {
                id: wrapper
                width: 180
                height: contactInfo.height
                color: "lightblue"

                Text {
                    id: contactInfo
                    text: name + ": " + number
                    color: "black"
                }

                MouseArea {
                    anchors.fill: parent
                    onPressed: {
                        myList.interactive = false
                        console.debug("onPressed")
                    }

                    onClicked:  {
                        myList.interactive = false
                        console.debug("onClicked")
                    }

                    onReleased: {
                        myList.interactive = true
                        console.debug("onReleased")
                    }
                }
            }
        }

        model: listModel
        delegate: contactsDelegate
        focus: true
    }
}