qtqmlstackview

qml Stackview: pop a page which is in another file


My Problem, is that I cant pop() a page out of the stack. Let me explain a little bit:

I have a page where a couple of items are displayed Each item has a button, which open a new page on top of the stackview, where i can edit my item. This EditPage has a close button - which should: yeah, you get it: close the page xD

which means that I want to pop the page from the stack

MainView.qml

// ...

StackView {
    id: _myStack
    initialItem: _myView

    GridView {
      id: _myView       
      model: // comes from c++
      delegate: MyItem{}
    }
}

MyItem.qml

Item{
  id: _myItem

  Button{
    id: _buttonEdit
    text: "Edit"
    onClicked{
      _stackview.push("../components/Edit.qml, {some properties I pass through}, MainView.Immediate)   
       // Edit.qml is in another folder, import is done
    }
  }
}

Edit.qml

Page{
  id: _editPage

  // some code

  Button {
    id: _closeButton

    onClicked{
      //
      // here I want to pop this window out of the stack (it is the top item on the stack)
      // _myStack.pop() doesn't work - because _myStack is not visible
    }
}

what can I do? i have already tried this in Edit.qml: bit i stuck at this point - where to bind the signal in MyItem.qml or?

Page{
  id: _editPage

  signal closeEditPage

  Button {
    id: _closeButton

    onClicked{
       closeEditPage()
    }
}

Solution

  • Rebind attached property StackView.view for easier access.

    Page{
      id: _editPage
    
      property StackView _myStack: StackView.view
    
      Button {
        id: _closeButton
    
        onClicked{
           _myStack.pop()
        }
    }