带有table的qml scrollview自动转到底部

qml scrollview with table automatically go to bottom

对于一个项目,我在 table 中显示数据,最新的数据是最相关的,所以我希望它是最可见的,但也应该可以看到较早的数据。 以下代码包含带有滚动条的视图。

    ScrollView{
        id:dataScrollView
        width: parent.width
        anchors.top: headerWrapper.bottom
        height:parent.height - headerWrapper.height

        ScrollBar.vertical.policy: ScrollBar.AlwaysOn;
        clip: true



        TableView{
            id: table
            columnSpacing: 1
            rowSpacing: 1
            clip: true
            implicitHeight: column.height - 33
            implicitWidth: column.width
            model: modelItem
            delegate: compColored
            onContentHeightChanged: {
                //new data has come in I want to scroll to the bottom
                console.log("update scroll to bottom" )
            }
        }
    }

我确切地知道将代码放在哪里,但我不知道如何使滚动条到达底部或如何设置滚动条的位置。 谁能告诉我如何设置滚动条的位置?

由于 TableView 继承了 Flickable,您可以简单地通过以下方式在视口中重新定位 contentItem:

 onContentHeightChanged: {
     table.contentY = table.contentHeight - table.height
 }

并且 Flickable 的 contentItem 底部区域将在视口中可见。