如何在 Qml 中更改 ListView 的滚动条位置?

6
我已经创建了一个带有滚动条的ListView。像这样:

enter image description here

以下是我的代码:
ListView {
    id: listView;
    ScrollBar.vertical: ScrollBar {
        id: bar
        //x :100 doesn't work
        active: true

      }
    }

在我的情况下,我想重置滚动条的位置。例如,将滚动条向右移动5像素。我尝试了设置"x:",但没有起作用。我该如何解决这个问题。

如果你要将滚动条向右移动,它会超出窗口。 - eyllanesc
移动它到左边怎么样?我只是想知道如何改变ListView中滚动条的位置。 - Ryou
2个回答

5
您可以将ScrollBar移动到ListView之外,并使用其idListView内引用它:
ListView {
    id: listView
    ScrollBar.vertical: bar
}

ScrollBar {
    id: bar
    active: true
    anchors {
        left: listView.left
        top: listView.top
        bottom: listView.bottom
        leftMargin: 100
    }
}

4

在加载ScrollBar后的一瞬间,您需要设置该属性,因为ListView会将其设置为默认位置。

ScrollBar.vertical: ScrollBar {
    id: sb
    active: true
    Component.onCompleted: x = 100
}

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接