【问题标题】:QML Swipeview dynamically add and remove pagesQML Swipeview 动态添加和删除页面
【发布时间】:2020-03-12 08:00:06
【问题描述】:

我看到了下面的问题。

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

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

我该如何解决这个问题?

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      相关资源
      最近更新 更多