【发布时间】:2020-08-02 16:01:50
【问题描述】:
我正在使用带有部分的 QML ListView,单击要删除动画的项目。代码如下:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
visible: true
width: 400
height: 400
ListView {
id: list
anchors.fill: parent
clip: true
spacing: 0
onContentYChanged: console.log("onContentYChanged: " + contentY)
onContentHeightChanged: console.log("onContentHeightChanged: " + contentHeight)
model: ListModel {
id: myModel
ListElement {name: "Item 1";type: "A"}
ListElement {name: "Item 2";type: "A"}
ListElement {name: "Item 3";type: "B"}
}
delegate: Rectangle {
width: parent.width
height: 50
color: (index % 2 == 1) ? "#5678a2" : "#88a345"
Text {
anchors.verticalCenter: parent.verticalCenter
text: name
}
MouseArea {
anchors.fill: parent
onClicked: {
console.log("remove: " + index + ", contentY:" + list.contentY)
myModel.remove(index)
}
}
}
section.property: "type"
section.delegate: Rectangle {
height: 30
Text {
anchors.verticalCenter: parent.verticalCenter
text: section
}
}
displaced: Transition {
NumberAnimation { properties: "x,y"; duration: 500; easing.type: Easing.OutCubic }
}
remove: Transition {
NumberAnimation { property: "opacity"; from: 1.0; to: 0; duration: 500 }
NumberAnimation { property: "scale"; from: 1.0; to: 0; duration: 500 }
}
}
}
当我点击第一个项目(项目 1)时,它被删除了,但项目 2 飞到了窗外。 ListView 将剩余项目显示在错误的位置。 ContentY 变为 80(之前是 Item 2 的 y 位置),而不是保持在 0。
qml: onContentHeightChanged: 300
qml: onContentHeightChanged: 240
qml: onContentHeightChanged: 210
qml: remove: 0, contentY:0
qml: onContentYChanged: 80
qml: onContentHeightChanged: 160
如果满足以下条件,它将正常工作:
- 删除除最上面的其他项目。
- 禁用部分或动画。
【问题讨论】:
-
你好。你还需要帮助吗?如果是这样,您能否澄清一下,>删除除最上面的其他项目之外的其他项目应该是不可删除的,究竟是什么意思?如果是这样,如果组中的元素很少,我应该能够删除第一个元素吗?
-
@MaximSkvortsov 当 ListView 中有很多项时,该错误仅在删除第一个(顶部)项时发生。