Search code examples
qtqmlstackview

How to push into a StackView recursively QML?


I have a StackView and an item which I want to push every time I click a button. It works only the first time I press a button, I can understand there is an issue with parent-child relationship. But how to fix it?

main.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    visible: true
    width: 640
    height: 480

    StackView{
        id: stackView
        anchors.fill: parent

        initialItem: Item {
            width: parent.width
            height: parent.height
            FirstLevel {
                myModel: 5
                onButtonClicked: {
                    stackView.push("qrc:/FirstLevel.qml", {myModel: 5})
                }
            }
        }
    }
}

FirstLevel.qml

import QtQuick 2.0
import QtQuick.Controls 1.4

Column {
    property var myModel

    signal buttonClicked()

    Repeater {
        model: myModel

        Button {
            text: model.index
            onClicked: {
                buttonClicked()
            }
        }
    }
}

UPDATE I

I want to push FirstLevel.qml item into a stackview every time I click a button in the FirstLevel.qml item.


Solution

  • You have to do the push in the FirstLevel.qml:

    main.qml

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    
    Window {
        visible: true
        width: 640
        height: 480
    
        StackView{
            id: stackView
            anchors.fill: parent
    
            initialItem: Item {
                width: parent.width
                height: parent.height
                FirstLevel {
                    myModel: 5
                }
            }
        }
    }
    

    FirstLevel.qml

    import QtQuick 2.0
    import QtQuick.Controls 1.4
    
    Column {
        property var myModel
        Repeater {
            model: myModel
            Button {
                text: model.index
                onClicked: {
                    stackView.push("qrc:/FirstLevel.qml", {myModel: myModel})
                }
            }
        }
    }