QML Swipeview可动态添加和删除页面



我看到了下面的问题。

QML Swipeview动态添加页面

但是,当我删除了项目(B(并添加了项目(B(时。

未添加"项目B"。

SwipeView {
id: swipeView
anchors.fill: parent
currentIndex: 0
Component.onCompleted: {
addItem(A)
addItem(B)
removeItem(B)
addItem(B)
}
}
PageIndicator {
id: indicator
count: {
console.log("swipeView.count : ", swipeView.count)
return swipeView.count
}
currentIndex: swipeView.currentIndex
anchors.bottom: swipeView.bottom
anchors.horizontalCenter: parent.horizontalCenter
}

console.log的结果("swipeView.count:",swipeView.caunt(

qml:swipeView.count:0

qml:swipeView.count:1

qml:swipeView.count:2

qml:swipeView.count:1

qml:swipeView.count:2

qml:swipeView.count:1

也就是说,如果再次添加已擦除的项目,则不会添加该项目。

我该怎么解决这个问题?

如文档所示,removeItem将销毁该项目。因此,当您第二次调用addItem时,B将为空。

您应该使用takeItem,而不是:

Window {
id: window
width: 400
height: 400
visible: true
Text {
id: a
text: "A"
}
Text {
id: b
text: "B"
}
SwipeView {
id: swipeView
anchors.fill: parent
currentIndex: 0
Component.onCompleted: {
addItem(a)
addItem(b)
takeItem(1);
addItem(b)
}
}
PageIndicator {
id: indicator
count: {
console.log("swipeView.count : ", swipeView.count)
return swipeView.count
}
currentIndex: swipeView.currentIndex
anchors.bottom: swipeView.bottom
anchors.horizontalCenter: parent.horizontalCenter
}
}

最新更新