0

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"))  }
    }
   }
deW1
  • 5,562
  • 10
  • 38
  • 54
srs
  • 121
  • 8

2 Answers2

1

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

Mitch
  • 23,716
  • 9
  • 83
  • 122
deW1
  • 5,562
  • 10
  • 38
  • 54
  • StackView.view seems to be empty / null. Thats the error gets thrown when I tried it – srs Dec 13 '19 at 21:18
  • You need to qualify it with the correct id. See [this](https://stackoverflow.com/a/31966706/904422) answer for an example. In this case it would be the root item (which would need to be given an id). I've updated the answer. – Mitch Dec 13 '19 at 23:31
0

Finally ended up creating each page in StackView as a component property and then pushing each of them using a signal. Added the signal to every page and connected it to main page where stackview existed . This answer helped https://stackoverflow.com/a/45354861/11288640

srs
  • 121
  • 8