当将QML ListView元素与其节属性结合使用时,我遇到了一个非常特殊的问题。
我使用的是Qt 4.8.6,但当我在Qt 5.3.1中尝试时,我也遇到了同样的问题。
以下代码也可以在旧版本的Qt中运行,只需将import语句更改为即可
导入QtQuick 1.0(适用于<Qt 4.7.4)
或
导入QtQuick 1.1(对于>=Qt 4.7.4)
这里有一个独立的用例来演示我的问题:
import QtQuick 2.2
Rectangle {
width: 800
height: 800
color: "black"
property int pageNumber: 1
property int totalPages: Math.ceil(animalListView.contentHeight/animalListView.height)
Text {
x: 2
y: 90
color: "Orange"
text: "Expected height: " + (animalListView.count*70 + (50*10))
font.pixelSize: 28
}
Text {
x: 2
y: 0
color: "Orange"
text: "Actual ContentHeight: " + animalListView.contentHeight
font.pixelSize: 28
}
Text {
x: 2
y: 30
color: "Orange"
text: "Actual ChildrenRectHeight: " + animalListView.childrenRect.height
font.pixelSize: 28
}
Text {
x: 2
y: 60
color: "Orange"
text: "Total model items (minus sections): " + animalListView.count
font.pixelSize: 28
}
Rectangle {
id: boundingRect
width: 640
height: 500
x: 20
y: 200
radius: 10
border.width: 1
border.color: "green"
color: "transparent"
// The delegate for each section header
Component {
id: sectionHeaderDelegate
Rectangle {
width: parent.width
height: 50 // this is the problem
color: "transparent"
Text {
anchors.left: parent.left
id: headerText
text: section
color: "red"
}
Rectangle {
anchors.fill: parent
border.color: "purple"
border.width: 1
color: "transparent"
}
}
}
ListModel {
id: animalsModel
ListElement { name: "1Parrot"; size: "Small" }
ListElement { name: "2Guinea pig"; size: "Small" }
ListElement { name: "3Dog"; size: "Medium" }
ListElement { name: "4Cat"; size: "Medium" }
ListElement { name: "5Elephant"; size: "Medium" }
ListElement { name: "6Parrot"; size: "Small" }
ListElement { name: "7Guinea pig"; size: "Small" }
ListElement { name: "8Dog"; size: "Medium" }
ListElement { name: "9Cat"; size: "Medium" }
ListElement { name: "10Elephant"; size: "Large" }
ListElement { name: "11Parrot"; size: "Large" }
ListElement { name: "12Guinea pig"; size: "Large" }
ListElement { name: "13Dog"; size: "Large" }
ListElement { name: "14Cat"; size: "Medium" }
ListElement { name: "15Elephant"; size: "Large" }
ListElement { name: "16Parrot"; size: "Small" }
ListElement { name: "17Guinea pig"; size: "Small" }
ListElement { name: "18Dog"; size: "Medium" }
ListElement { name: "19Cat"; size: "Medium" }
ListElement { name: "20Elephant"; size: "Large" }
}
ListView {
id: animalListView
anchors.fill: parent
anchors.margins: 10
clip: true
interactive: true
flickableDirection: Flickable.VerticalFlick
boundsBehavior: Flickable.StopAtBounds
model: animalsModel
delegate: Item {
width: parent.width
height: 70
Text {
text: name
color: "green"
}
Rectangle {
anchors.fill: parent
border.color: "yellow"
border.width: 1
color: "transparent"
}
}
section.property: "size"
section.criteria: ViewSection.FullString
section.delegate: sectionHeaderDelegate
}
}
Rectangle {
anchors.top: boundingRect.top
anchors.left: boundingRect.right
anchors.leftMargin: 20
width: 40
height: 40
color: "blue"
MouseArea {
anchors.fill: parent
onClicked: {
if (pageNumber > 1) {
animalListView.contentY -= animalListView.height;
animalListView.returnToBounds();
--pageNumber;
}
}
}
enabled: (!animalListView.atYBeginning)
visible: !(animalListView.atYBeginning && animalListView.atYEnd)
Text {
anchors.centerIn: parent
font.family: "Wingdings 3"
font.pixelSize: 40
text: "Ç" // Up arrow
}
}
Text {
visible: totalPages > 1
anchors.left: boundingRect.right
anchors.verticalCenter: boundingRect.verticalCenter
width: 100
height: 20
font.pixelSize: 18
horizontalAlignment: Text.AlignHCenter
color: "red"
text: qsTr("%1 of %2").arg(pageNumber).arg(totalPages)
}
Rectangle {
anchors.bottom: boundingRect.bottom
anchors.left: boundingRect.right
anchors.leftMargin: 20
width: 40
height: 40
color: "orange"
MouseArea {
anchors.fill: parent
onClicked: {
if (pageNumber < totalPages) {
animalListView.contentY += animalListView.height;
++pageNumber;
}
}
}
enabled: (!animalListView.atYEnd)
visible: !(animalListView.atYBeginning && animalListView.atYEnd)
Text {
anchors.centerIn: parent
font.family: "Wingdings 3"
font.pixelSize: 40
text: "È" // Down arrow
}
}
}
我使用ListView来显示动物模型的列表,按它们的大小进行分类。为了在视图中实现这种分类,我使用了ListView的节.property、段.criteria和部分.delete属性,如上面给出的代码中所实现的。
(注意:请忽略我提供给ListView的模型没有排序的事实,我知道这会在ListView中创建大量重复的类别条目。这与这里的重点无关。)
当模型数量超过ListView的可见区域时,我将使用属性totalPages来计算有多少完整的ListView页面可供导航。向上箭头和向下箭头按钮只是将ListView的内容.Y分别递减和递增ListView的高度。
问题是ListView的contentHeight没有保持静态,它正在动态更改,并导致我的 值得注意的是,当且仅当我为sectionHeaderDelegate矩形设置了高度时,才会发生这种行为。如果我注释掉height语句(height:50),ListView的contentHeight将保持静态,正如预期的那样——缺点是部分标题/类别现在位于模型文本的顶部,这根本没有用处。 所以我的问题是,为什么QML ListView元素的contentHeight在我使用高度设置为非零值的部分委托时会动态更改 此外,出于测试目的,我在ListView中保留了以下属性,ListView应该与向上/向下箭头一起使用: interactive: true
flickableDirection: Flickable.VerticalFlick
boundsBehavior: Flickable.StopAtBounds
我知道这很古老,但无论如何我都会在这里回答,因为我正在寻找解决方案;
如果你的物品有一个固定的高度,你可以通过公式设置值来动态设置容器的高度:
MyContainerWithListItems {
height: MyModel.items.length * height
}
如果你有可变高度的项目,这将更加困难;解决方案可能是让onChange事件触发一个函数,该函数会遍历项目并手动添加高度。
这是因为ListView通过当前可见的项来估计其contentHeight。检查当您的分区无法分组时会发生什么。ListView避免实例化每个项,因此它不知道不可见内容的正确大小。看看这根线。
声明一个全局属性,用于存储列表视图的高度:
property int propertyListViewHeight:0
将ListView放入透明项中,高度等于全局属性propertyListViewHeight
:
Item {
id: rectangleEntities
width: parent.width
height: propertyListViewHeight
ListView {
id: listViewEntities
anchors.fill: parent
model: entityModel
delegate: listDelegate
focus: true
boundsBehavior: Flickable.StopAtBounds
highlightMoveDuration: 1000
highlightMoveVelocity: 1000
}
}
在您的委托定义中添加onCompleted信号,然后将列表中每个项目的高度添加到全局属性propertyListViewHeight
组件{id:listDelegate矩形{id:容器width:pparent.width
Component.onCompleted: {
propertyListViewHeight += height
}
}
}