Search code examples
qtqmlqtquickcontrols2stackview

How to open new page from page which is open from stackview in Qt


I have StackView in main.qml.I pushed menu.qml file from main.qml using stackview. I'm trying to access stackview in menu.qml file to open new item. Is there a way with which we can push component/items with properties using stackview? My components are basically.qml files for different views

 ApplicationWindow {
id: settingsWindow

        StackView {
            id: stack
            initialItem: view

            Component {
                id: view

                MouseArea {

                    onClicked: stack.push(view)
                }
            }
        }

      Button{
        id: button1
        onClicked: {
                stack.pop(StackView.Immediate)
                stack.push (Qt.resolvedUrl("menu.qml"))
        }
       }
    }

menu.qml

 Item {
 Button{
 id: button1  
 onclicked : {  stack.push (Qt.resolvedUrl("new.qml"))  }
    }
   }

Solution

  • Assuming you mean you want to access the StackView object from withing pages you pushed on it.

    StackView has an attached property, which lets you obtain a reference to the view that owns the page.

    Long story short, in Menu.qml you can do:

    Item {  
        id: root                                                               
        Button {                                                            
            id: button1                                                    
    
            onClicked: { root.StackView.view.push(Qt.resolvedUrl("new.qml")) } 
        }                                                                  
    }        
    

    https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html#view-attached-prop