使矩形宽度填充ScrollView

时间:2015-11-23 04:36:11

标签: qml qt5 qtquick2 qtquickcontrols

我试图让ScrollView中的内容扩展到width以适应屏幕。 ScrollView锚定在主窗口。

例如,Rectangle

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

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

width: mainWindow.width - 20

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

有没有办法自动了解ScrollView内的可用空间?

1 个答案:

答案 0 :(得分: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
    }
}

原始问题的解决主要是由于width的{​​{1}}属性设置为Rectangleparent.parent.width,因为它更合适。后者肯定更好,只要滚动区域的精确视口的scrollView.viewport.width而不是父width(通常不保证只包含此width)。