1

我正在尝试在 a 中添加一些东西ScrollViewwidth适应屏幕。ScrollView锚定到主窗口。

例如,一个Rectangle

ScrollView {
    anchors.fill: parent //mainWindow
    Rectangle {
        color: "light grey"
        height: 1000
        width: mainWindow.width
    }
}

但是当垂直滚动条出现时,它会遮挡Rectangle. 我可以通过使用魔法常数来修复它:

width: mainWindow.width - 20

但是如果有人在他们的计算机上有更大的滚动条呢?当垂直滚动条不可见时,它也会在右侧留下一个难看的空白空间。

有没有办法自动了解 a 内部的可用空间是什么ScrollView

4

1 回答 1

2

无需显式调整滚动条。你可以让它填满整个可用的父空间左右。如果你想指定边距:

ScrollView {
    id: scrollView
    anchors.fill: parent // mainWindow ?
    anchors.centerIn: parent // anchoring as asked
    anchors.margins: 20

    contentItem:
        Rectangle {
        id: rectScroll
        width: scrollView.viewport.width  // set as viewport
        height: 1000 // set to what you need
    }
}

最初的问题主要是由于set to的width属性或因为它更合适而得到解决。后者肯定更好,只要滚动区域的精确视口而不是父级(通常不保证只包含 this )。Rectangleparent.parent.widthscrollView.viewport.widthwidthwidthScrollView

于 2015-11-23T04:43:59.240 回答