从Qml中弹出时从Stackview项获取值或属性

问题描述 投票:0回答:1

当Qml中弹出Stackview项目时,有什么方法可以获取价值或属性?当它在以下项目中弹出到“ Profile.qml”时,我想在“ EditProfile.qml”中获得编辑后的名称。

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()
        }
    }
}     
qt qml
1个回答
0
投票

仔细阅读QT手册后,我找到了答案。 push功能返回被推送的项目。所以:

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()
            }
        }
    }
}     
© www.soinside.com 2019 - 2024. All rights reserved.