Qt 在将最后一个项目添加到listview时,如何保持它们在视图中?
我有一个ListView,可以在其中动态添加ListElements。ListView最多可以显示10个项目,因此我还有一个滚动条。当我添加第11+项时,我总是希望它滚动到视图中Qt 在将最后一个项目添加到listview时,如何保持它们在视图中?,qt,listview,qml,scrollview,Qt,Listview,Qml,Scrollview,我有一个ListView,可以在其中动态添加ListElements。ListView最多可以显示10个项目,因此我还有一个滚动条。当我添加第11+项时,我总是希望它滚动到视图中 ListView { id: logListView delegate: logListViewDelegate model: logListModel anchors.fill: parent ScrollBar.vertical: ScrollBar {} } List
ListView {
id: logListView
delegate: logListViewDelegate
model: logListModel
anchors.fill: parent
ScrollBar.vertical: ScrollBar {}
}
ListModel {
id: logListModel
}
Component {
id: logListViewDelegate
Item {
height: 44
width: logListView.width
Text {
id: countText
width: 18
font {
pixelSize: 16
family: variables.globalFont
}
color: colors.foregroundColor3
text: index+1
anchors {
left: parent.left
leftMargin: 7
verticalCenter: parent.verticalCenter
}
}
Text {
id: timeText
width: 96
horizontalAlignment: Text.AlignRight
font {
pixelSize: 24
family: variables.globalFont
}
color: colors.foregroundColor1
text: time
anchors {
left: countText.right
verticalCenter: parent.verticalCenter
}
}
Text {
id: unitText
width: 18
font {
pixelSize: 16
family: variables.globalFont
}
color: colors.foregroundColor3
text: unit
anchors {
left: timeText.right
leftMargin: 6
bottom: timeText.bottom
bottomMargin: 2
}
}
}
}
我在listview外有一个按钮,单击该按钮时只执行以下操作:
logListModel.append({
time: myTime, unit: myUnit
})
新项目刚刚添加到列表的底部,并且在超过10个时隐藏。添加项目时,我希望列表自动滚动到该项目。在ListView中,当模型更改时调用onCountChanged时,通过更改currentIndex滚动到底部:
ListView {
id: logListView
delegate: logListViewDelegate
model: logListModel
anchors.fill: parent
ScrollBar.vertical: ScrollBar {}
onCountChanged: {
var newIndex = count - 1 // last index
positionViewAtEnd()
currentIndex = newIndex
}
}