【问题标题】:QML ListView: How to disable autoscrolling when new elements inserted?QML ListView:插入新元素时如何禁用自动滚动?
【发布时间】:2023-04-08 21:12:01
【问题描述】:

有谁知道,当我在其头部添加一些元素时,如何防止向下滚动 QML ListView?我想以类似 Twitter 的方式更新 ListView,当它始终保持其位置并且只能显式滑动时。

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    实际上insert 函数完成了这项工作。您可以将其插入顶部或任何所需位置,例如modelName.insert(0,{object});。一个工作示例是here。

    import QtQuick 1.0
    
    Rectangle {
        id: rectangle1
        width: 320
        height: 480
    
        ListModel {
            id: cModel
            ListElement {
                name: "Bill Smith"
                number: "555 3264"
            }
            ListElement {
                name: "John Brown"
                number: "555 8426"
            }
            ListElement {
                name: "Sam Wise"
                number: "555 0473"
            }
        }
        ListView {
            id: list_view1
            width: rectangle1.width
            height: rectangle1.height - 40
            anchors.horizontalCenter: parent.horizontalCenter
            delegate: Text {
                text: name + ": " + number
            }
            model: cModel
        }
    
        Rectangle {
                id: rectangle2
                width: 320
                height: 40
                color: "#ffffff"
                anchors.top: list_view1.bottom
    
                Text {
                    id: text1
                    text: qsTr("Click to add!")
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.verticalCenter: parent.verticalCenter
                    font.pixelSize: 16
                    MouseArea {
                        id: mouse_area1
                        anchors.fill: parent
                        onClicked: addNewItemTop();
                    }
                }
            }
    
        function addNewItemTop()
        {   var i = Math.random();
            cModel.insert(0,{"name" : "New Number", "number":i.toString()});
        }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多