qtqmlflickable

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


I want to disable the dragging of my List view using pressing and holding a mouse button while moving the cursor. I am trying to implement some other feature such as multiple selection using this command. I would only like the scrolling to be enabled. The interactive property of the flickable totally disables the movement of the view. Is there some workaround for this?


Solution

  • I guess there are some ways to achieve what you need, but the following solution works.

    The idea is to have a MouseArea and set the interactive property to false when the signals onPressed and onClicked are emitted. interactive should be set to true again in the onReleased handler.

    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
        }
    }