我看到了下面的问题。
QML Swipeview dynamically add pages
但是,当我做removeItem(B)和addItem(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.count)
qml: swipeView.count :0
qml: swipeView.count :1
qml: swipeView.count :2
qml: swipeView.count :1
qml: swipeView.count :2
qml: swipeView.count :1
也就是说,如果再次添加已删除的项目,则不会添加该项目。
我该如何解决这个问题呢?
发布于 2020-03-12 21:20:36
正如documentation中所指出的,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
}
}https://stackoverflow.com/questions/60649787
复制相似问题