一、前言
本文主要讲解,通过外部自定义滑动条与 TableView 绑定在一起
二、示例
TableView {
id: tableView
}
ScrollBar {
id: verticalBar
hoverEnabled: true
active: hovered || pressed
orientation: Qt.Vertical
size: infoTableView.height / infoTableView.flickableItem.contentHeight
width: 10
height: parent.height
anchors.bottom: bottomCheckGroup.top
policy: ScrollBar.AlwaysOn
onPositionChanged: {
infoTableView.flickableItem.contentY = position * (infoTableView.flickableItem.contentHeight) -
__rowTitleHight
}
}
ScrollBar {
id: horizonBar
hoverEnabled: true
active: hovered || pressed
orientation: Qt.Horizontal
size: infoTableView.width / infoTableView.flickableItem.contentWidth
width: frame.width
height: 10
anchors.bottom: bottomCheckGroup.top
policy: ScrollBar.AlwaysOn
onPositionChanged: {
infoTableView.flickableItem.contentX = position * (infoTableView.flickableItem.contentWidth)
}
}
Connections{
target: infoTableView.flickableItem
onContentXChanged: {
horizonBar.position = infoTableView.flickableItem.contentX /
infoTableView.flickableItem.contentWidth
}
}
Connections{
target: infoTableView.flickableItem
onContentYChanged: {
verticalBar.position = (infoTableView.flickableItem.contentY + __rowTitleHight) /
infoTableView.flickableItem.contentHeight
}
}
这里简单讲解一下
-
与Qt5.12版本不同,这个版本只能通过 flickableItem 来获取与 flickable 相关的内容
-
__rowTitleHight 是指标题的高,这里必须说明就是在修改 contentY 的时候一定要减去标题头部的高,也就是 contentY = -(__rowTitleHight)是起始位置(__rowTitleHight 是自己定义的变量为 60)