Search code examples
qtqmlstackview

How to unwind StackView till the second last item in QML


I want to unwind till the second last item in StackView. The documentation states it should be as easy as stackView.pop({item: itemName}), but this is working as if I'm doing stackView.pop(). What is the correct way of unwinding till a specific item?

My Qt version is 5.14.2.


Solution

  • The documentation you linked is the one for QQC1's StackView. For Qt Quick Controls 2 StackView, the pop function is a bit different : https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html#pop-method

    You can call it like so : stackView.pop(itemName). Note that itemName need to be an instance of the item pushed on the stack. If you push Components on the StackView, trying to pop with a reference to the Component won't work.

    // 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);