首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QML Swipeview动态添加和删除页面

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

Stack Overflow用户
提问于 2020-03-12 16:00:07
回答 1查看 613关注 0票数 0

我看到了下面的问题。

QML Swipeview dynamically add pages

但是,当我做removeItem(B)和addItem(B)时。

未添加“项目B”。

代码语言:javascript
复制
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

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

我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-12 21:20:36

正如documentation中所指出的,removeItem将销毁该项目。因此,当您第二次调用addItem时,B将为空。

您应该使用takeItem,而不是:

代码语言:javascript
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60649787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档