QML ListView contentY 属性 在模型开头添加新元素后显示不正确的值
QML ListView contentY property show an incorrect value after adding a new element to the beginning of the model
这种情况出现在使用 C++ 模型时,但它也在纯 QML 中重现。
- 整页
ListView
组件
- Delagat 是最简单的,消除了错误的计算——一个固定高度和宽度的矩形。加上用于显示元素索引的文本。
- 数据模型是根据多个值在 QML 中设置的。在下面的示例中,有六个。
- 表单包含有关 ListView 中上下文 Y 属性 当前值的信息的文本。
- 表单上有一个按钮,可以将新元素添加到模型的开头。
问题的本质。
如果 ListView 位于第一个元素上,则将新元素添加到模型的开头不会出错。
如果滚动 ListView 以致第一个元素不可见,则将新元素添加到模型的开头会导致 contentY
[=47 的值不正确(在我看来) =].如果我们在添加新元素后将 ListView
滚动到开头,我们会得到 contextY
=-189 而不是预期的 contextY
=0。其中189是代表的身高。
我花了很多时间寻找一个最小的例子(
当 运行 在 Windows 和 Android 上时,此效果在 Qt 5.14.2 上重现。
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.12
Window {
visible: true
width: 379
height: 680
title: qsTr("Hello ListView ContentY")
ListModel {
id: idListModel
ListElement { name: "A" }
ListElement { name: "B" }
ListElement { name: "C" }
ListElement { name: "D" }
ListElement { name: "E" }
ListElement { name: "F" }
}
ListView {
id: idRequestsListView
anchors.fill: parent
model: idListModel
delegate: Rectangle {
id: idRequestSmallCardMainRect
height: 189
width: parent.width
color: "white"
border.color: "gray"
border.width: 1
Text {
x: 5
text: "index: " + (model.index + 1) + " (" + model.name + ")"
font.pixelSize: 18
}
}
Text {
anchors.left: parent.left
anchors.leftMargin: 170
text: "contextY: " + parent.contentY + "\n"
+ "contentHeight: " + parent.contentHeight + "\n"
+ "count: " + idListModel.count
font.pixelSize: 15
color: "blue"
}
}
Button {
anchors.centerIn: parent
text: "prepend item"
onClicked: {
console.log("idListModel.size begin:", idListModel.count)
idListModel.insert(0, { name: "G" })
console.log("idListModel.size end:", idListModel.count)
}
}
}
我知道您不希望 contentY
成为 -189
,但实际上它需要与 originY
一起考虑。当您将项目添加到列表时,originY
也会更新。当您滚动到顶部并减去 contentY - originY
时,您应该得到 0
。但是他们的个人价值观可以是任意的。
这种情况出现在使用 C++ 模型时,但它也在纯 QML 中重现。
- 整页
ListView
组件 - Delagat 是最简单的,消除了错误的计算——一个固定高度和宽度的矩形。加上用于显示元素索引的文本。
- 数据模型是根据多个值在 QML 中设置的。在下面的示例中,有六个。
- 表单包含有关 ListView 中上下文 Y 属性 当前值的信息的文本。
- 表单上有一个按钮,可以将新元素添加到模型的开头。
问题的本质。 如果 ListView 位于第一个元素上,则将新元素添加到模型的开头不会出错。
如果滚动 ListView 以致第一个元素不可见,则将新元素添加到模型的开头会导致 contentY
[=47 的值不正确(在我看来) =].如果我们在添加新元素后将 ListView
滚动到开头,我们会得到 contextY
=-189 而不是预期的 contextY
=0。其中189是代表的身高。
我花了很多时间寻找一个最小的例子(
当 运行 在 Windows 和 Android 上时,此效果在 Qt 5.14.2 上重现。
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.12
Window {
visible: true
width: 379
height: 680
title: qsTr("Hello ListView ContentY")
ListModel {
id: idListModel
ListElement { name: "A" }
ListElement { name: "B" }
ListElement { name: "C" }
ListElement { name: "D" }
ListElement { name: "E" }
ListElement { name: "F" }
}
ListView {
id: idRequestsListView
anchors.fill: parent
model: idListModel
delegate: Rectangle {
id: idRequestSmallCardMainRect
height: 189
width: parent.width
color: "white"
border.color: "gray"
border.width: 1
Text {
x: 5
text: "index: " + (model.index + 1) + " (" + model.name + ")"
font.pixelSize: 18
}
}
Text {
anchors.left: parent.left
anchors.leftMargin: 170
text: "contextY: " + parent.contentY + "\n"
+ "contentHeight: " + parent.contentHeight + "\n"
+ "count: " + idListModel.count
font.pixelSize: 15
color: "blue"
}
}
Button {
anchors.centerIn: parent
text: "prepend item"
onClicked: {
console.log("idListModel.size begin:", idListModel.count)
idListModel.insert(0, { name: "G" })
console.log("idListModel.size end:", idListModel.count)
}
}
}
我知道您不希望 contentY
成为 -189
,但实际上它需要与 originY
一起考虑。当您将项目添加到列表时,originY
也会更新。当您滚动到顶部并减去 contentY - originY
时,您应该得到 0
。但是他们的个人价值观可以是任意的。