0

My app has a stackview that has two QML files. And I need to navigate from one QML file to another from inside that QML file itself where I won't have access to stackview to push or pop. How should I design this?

Main.qml

Rectangle
{
   id: mainBkg
   color: "white"
   width: Screen.width
   height: Screen.height

    //Tickes every globalTimer.interval
    property int globalTick: 0


     Statusbar
     {
        width: Screen.width
        height: Screen.height*0.2
        id: statusBar
        Rectangle
        {
         width: parent.width
         height: parent.height*0.2
         anchors.fill: parent
         color: "green"

         Text {
            id: t
            text: qsTr("QMLfile1")

         }
       } 
     }//end of statusbar

    StackView {
        id: stackView
        anchors.top: statusBar.bottom
        anchors.centerIn: parent

        //What should be here? So that stackView
        //will be available inside the loaded items.   
        initialItem: 
    } 
}

Following are the two QML files:

QMLfile1.qml

Rectangle
{
    width: parent.width
    height: parent.height*0.2

    anchors.fill: parent
    color: "green"


    Text {
        id: t
        text: qsTr("QMLfile1")

    }
    MouseArea:{
      onClicked: //move to other QML file in stackview
    }
}

I have another QML file like the above one.

jxgn
  • 741
  • 2
  • 15
  • 36

1 Answers1

1

You have tons of options.

One would be, to add a signal to the root-element of the QMLfile1.qml, and connect to it in the main.qml. The change will be performed there.

You can also add a property var callback to the root-element of the QMLfile1.qml where you inject the push-method of the StackView when instantiating it.

Finally you can just not shadow the id of the StackView and access it across file boundaries. I generally don't like that.

  • Hi thanks. Can you please provide me an example code? – jxgn Nov 10 '17 at 06:40
  • Sorry, I have very little time right now. Could you outline your planed usecase a bit more? That would help me to know, which option suits you the best... If any at all. It might be very well, that a `StackLayout` or `SwipeView` might be more what you are looking for. – derM - not here for BOT dreams Nov 11 '17 at 09:36
  • Thanks :) I figured it out how to do it. I created components of each QML file in the QML file where I have the StackView. – jxgn Nov 13 '17 at 08:38