【问题标题】:How to unwind StackView till the second last item in QML如何展开 StackView 直到 QML 中的倒数第二个项目
【发布时间】:2021-09-09 09:23:37
【问题描述】:

我想放松到 StackView 中的倒数第二个项目。 documentation 声明它应该像 stackView.pop({item: itemName}) 一样简单,但这就像我在做 stackView.pop() 一样工作。展开到特定项目的正确方法是什么?

我的 Qt 版本是 5.14.2。

【问题讨论】:

    标签: qt qml stackview


    【解决方案1】:

    您链接的文档是用于 QQC1 的 StackView 的文档。对于 Qt Quick Controls 2 StackView,弹出功能有点不同:https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html#pop-method

    你可以这样称呼它:stackView.pop(itemName)。请注意,itemName 必须是压入堆栈的项目的实例。如果您在 StackView 上推送组件,则尝试使用对组件的引用来弹出是行不通的。

    // this works :
    stackView.push(itemInstance);
    ...
    stackView.pop(itemInstance);
    
    // this doesn't work:
    stackView.push(component);
    ...
    stackView.pop(component);
    
    // you could store the instance of the item created from the component like so:
    createdItem = stackView.push(component);
    ...
    stackView.pop(createdItem);
    
    // or use StackView.find
    const itemToPop = stackView.find(item => return item.objectName === "pop me");
    stackView.pop(itemToPop)
    
    // or StackView.get to get an item from its position in the stack:
    const itemToPop = stackView.get(1);
    stackView.pop(itemToPop);
    

    【讨论】:

    • 我犯了一个愚蠢的错误,使用Component 而不是Item。感谢您指出我可能出了什么问题,以及如果使用带有StackView 的组件,如何解决问题。
    猜你喜欢
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2016-01-15
    • 1970-01-01
    • 2021-01-06
    相关资源
    最近更新 更多