qml 滚动视图,表格自动转到底部

问题描述 投票:0回答:2

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

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

我确切地知道在哪里放置代码,但我不知道如何使滚动条转到底部或如何设置滚动条的位置。 有人可以告诉我如何设置滚动条的位置吗?

qt qml qtquickcontrols2
2个回答
4
投票

由于

TableView
继承了
Flickable
,您只需通过以下方式即可在视口中重新定位 contentItem:

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

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


0
投票

在Qt6中,最简单的解决方案是这样的:

TableView {
    ...

    onRowsChanged: {
        positionViewAtRow(rows - 1, TableView.AlignVCenter)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.