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

Ryo*_*you 6 qt qml qt5

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

在此输入图像描述

这是我的代码,

ListView {
    id: listView;
    ScrollBar.vertical: ScrollBar {
        id: bar
        //x :100 doesn't work
        active: true

      }
    }
Run Code Online (Sandbox Code Playgroud)

就我而言,我想重置滚动条的位置。例如,将滚动条向右移动 5 个像素。我尝试设置“x:”,但没有成功。我该如何解决我的问题。

aug*_*gre 5

您可以移动ScrollBar的外部ListView并使用其在内部引用ListViewid

ListView {
    id: listView
    ScrollBar.vertical: bar
}

ScrollBar {
    id: bar
    active: true
    anchors {
        left: listView.left
        top: listView.top
        bottom: listView.bottom
        leftMargin: 100
    }
}
Run Code Online (Sandbox Code Playgroud)


eyl*_*esc 4

您必须在加载后立即建立该属性,ScrollBar因为它将ListView其设置为默认位置。

ScrollBar.vertical: ScrollBar {
    id: sb
    active: true
    Component.onCompleted: x = 100
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述