我不能让你的代码frag工作-它看起来像你的代码应该依赖于内容你的 ScrollView
但这不是在代码中包含frag,它可能缺少其他一些参考文献。
无论如何,我建议在接近这有点不同-改变 ScrollView
's的内容的宽度的基础上是否 ScrollBar
是可见的。 我也设置 ScrollBar
政策而不是知名度。 我已经创建了一个全面的例子可以在其中添加或删除内容采用一个滑动对示范:
import QtQuick 2.15
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
ApplicationWindow {
id: root
visible: true
height: 500
width: 500
ColumnLayout {
anchors {
fill: parent
}
Slider {
// use slider to add delegates to the ScrollView to toggle the scroll bar visibility
id: slider
to: 20
}
ScrollView {
id: scroll
Layout.fillHeight: true
Layout.fillWidth: true
ScrollBar.vertical.policy: scrollBarVisible ? ScrollBar.AlwaysOn : ScrollBar.AlwaysOff
property bool scrollBarVisible: scroll.contentHeight > scroll.height
ColumnLayout {
width: scroll.scrollBarVisible ? scroll.width - scroll.ScrollBar.vertical.width : scroll.width // change the width of the
Repeater {
model: slider.value
delegate: Rectangle {
color: "tomato"
Layout.fillWidth: true
Layout.preferredHeight: 150
}
}
}
}
}
}
有一点要注意,虽然-你的 ScrollView
内容不能有其高度依赖于它的宽度,例如,如果内容有一些 Text
包装,如果没有足够宽,从而导致它获得更高当宽度下降。 这将回到无限的环境。