如何解决ScrollView内ColumnLayout内的ListView未填充可用的高度/宽度
我想在TextFeild,CheckBoxes和一些Button中显示一些数据,然后显示很长的列表。我希望整个页面都是可滚动的,而不仅仅是ListView。我该如何实现?
这是我到现在为止的内容:-
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
ScrollView { //comment out this
anchors.fill: parent //comment out this
ColumnLayout {
anchors.fill: parent
spacing: 5
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 100
color: "green"
}
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 100
color: "red"
}
ListView {
model : 20
Layout.fillWidth: true
Layout.fillHeight: true
delegate: Frame {
Label {
text: modelData + "th Item"
}
}
}
}
} //comment out this
}
这不起作用。它仅显示ListView的一个单元格,并且不能滚动。如何使ListView填充高度/宽度并使整个页面可滚动?
解决方法
第一个问题是ScrollView
的内容与ScrollView
的高度相同时无法滚动。它需要内容更大。否则,没有任何内容可滚动。因此,适合父母身高的ColumnLayout
无效。在这种情况下,Column
比ColumnLayout
更好,因为它将增长到其子代的高度,而不是将其子代调整为自己的大小。
第二个问题是ListView
不能正确计算其高度。我不确定为什么不这样做,但是我可以使用childrenRect.height
使您的示例正常工作。
ScrollView {
anchors.fill: parent
Column {
width: parent.width
spacing: 5
Rectangle {
width: parent.width
height: 100
color: "green"
}
Rectangle {
width: parent.width
height: 100
color: "red"
}
ListView {
model : 20
width: parent.width
height: childrenRect.height
delegate: Frame {
Label {
text: modelData + "th Item"
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。