0
votes

Is there any way to get value or property from Stackview item when it popped in Qml? I want to get the edited name in 'EditProfile.qml' when it popped to 'Profile.qml' in the below project.

main.qml

StackView {
    id: stackView
    Component.onCompleted: push('Profile.qml', {name : 'David'})
}

Profile.qml

Page {
    property string name: ''
    Column {
        Text {                
            text: 'name' + name
        }
        Button {
            text: 'Edit'
            onClicked: stackView.push('EditProfile.qml', {name : name})
        }
    }
}        

EditProfile.qml

Page {
    property alias name: txtName.text
    Column {
        TextEdit {         
            id: txtName
            text: name
        }
        Button {
            text: 'Back'
            onClicked: stackView.pop()
        }
    }
}     
1

1 Answers

0
votes

After reading QT manual carefully, I found the answer. The push function returns the item which is pushed. so:

Profile.qml

Page {
    id: root
    property string name: ''
    Column {
        Text {                
            text: 'name' + name
        }
        Button {
            text: 'Edit'
            onClicked: {
                var item = stackView.push('EditProfile.qml', {name : name})
                item.exit.connect(change);
                function change(text) {
                    item.exit.disconnect(change);
                    root.name = text;  
                }
            }
        }
    }
}      

EditProfile.qml

Page {
    signal exit(var text)
    property alias name: txtName.text
    Column {
        TextEdit {         
            id: txtName
            text: name
        }
        Button {
            text: 'Back'
            onClicked: {
                exit(txtName.text)
                stackView.pop()
            }
        }
    }
}