StackView - Remove A View Item Without Disturbing Current View



  • I'm using a StackView for screen navigation. Currently a couple views are pushed() onto the stack. Now due to a button push the middle views in the stack should be removed so that when the user pushes the "back" button, unwanted stackview states are skipped.

    // remove the "b" view from the stack..back button returns to "a"
    // stack should go from [a,b,c] to [a,c]

    This works per the documentation, BUT..the catch is the program's current view states variables, animations, etc MUST BE PRESERVED..but aren't.

    // sorta works but all current state info, etc is lost for the current "c" view

    stackView.clear()
    stackView.push( [ {item: Qt.resolvedUrl("qrc:/a.qml" ), properties: { objectName: "" }},
                      {item: Qt.resolvedUrl( "qrc:/c.qml" ), properties: { objectName: "c" }}
                    ] );
    

    So I tried this. Saving the current StackView item as a var, then adding it back in after the clear. Doesn't work..blank view.

    //did not work..to get the current "c" item in its current state

    var theCurentItem = stackView.find( function(item) { return item.name === "c"; } )
    stackView.clear()
    stackView.push( [ {item: Qt.resolvedUrl("qrc:/a.qml" ), properties: { objectName: "" }},
                      {item: theCurentItem, properties: { objectName: "c" } }
                    ] );
    

    Any thoughts or help appreciated...

    Edited - Please follow Markdown syntax rules - p3c0



  • Not really solved but a work-around is to:

    • save the state (ie any variable(s) of interest) in the last item on the stackview
    • clear the stack
    • push back all items except the one being "removed"
    • in the Component.onCompleted of the last item, restore the saved state

    Ulgy...but works if you only have a few variables that need to be preserved on the last item in the stackview.

    I guess another way would be to:

    • have a "skip me" boolean in each item pushed
    • evaluate the boolean after a pop
    • immediately pop again if "true".

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.