0

I see the below question.

QML Swipeview dynamically add pages

But, when i did removeItem(B) and addItem(B).

"item B" was not added.

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
}

Result of 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

That is, if an item that has been erased is added again, the item is not added.

How can I fix this?

ryu
  • 35
  • 7

1 Answers1

2

As indicated in the documentation, removeItem will destroy the item. So, B will be null when you call addItem the second time.

You should use takeItem, instead:

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
    }
}
Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37