(QT)如何在 qml 中使滚动条高度恒定

  • 本文关键字:滚动条 高度 qml QT qt qml
  • 更新时间 :
  • 英文 :


我试图使滚动条的高度恒定,而不管列表视图中的元素数量如何。但我没有找到任何属性来做到这一点。请建议如何做到这一点。提前谢谢。

代码片段:

Listview {
    id:mylistview
    height :400
    Width:500
    ScrollBar.vertical: scbr
    model :mymodel
    delegate:del{} 
} 
ScrollBar
{ 
    id: scbr 
    active: true
    interactive : true 
    hoverEnabled: true
    orientation :Qt.vertical 
    snapMode : ScrollBar.NoSnap
    contentItem: 
        Rectangle {  
        implicitWidth: 60                           
        implicitHeight: 100
        color: "#ff0000" 
    }
}

在这里,即使我们将隐式宽度设置为60。但是当模型具有非常大的数据时,滚动条图像会将其高度更改为非常小。

我创建了一个滚动条,它更像是一个滚动指示器,因为它不控制视图。它可以以指示您的视图相对于完整内容的当前位置的方式进行定位。

让我们考虑一个具有高度为"h"的委托的列表。您可以创建一个具有所需高度和宽度的矩形(例如,如示例中所示,高度为 100,宽度为 60(。

x 坐标 -> 列表宽度 - 滚动指示器宽度(本例中为 60(

y 坐标将是相对属性,它将是(list.contentY * (list.height-indicator.height((/(list.height - delegate.height(

示例如下:

Rectangle {
             id: indicator
             x: 0
             y: ((list.contentY) * (list.height - indicator.height)) / (list.height - delegate.height)
             width: 10
             height: 150
             color: "blue"
             radius: 10
         }
其中,列表是列表的 ID

,委托是视图中代理的 ID。

我不确定它是否适合您的确切目的,但我必须以这种方式继续保持固定大小滚动条的类似要求。

最新更新