简体   繁体   English

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

[英]Get value or property from Stackview item when it popped in Qml

Is there any way to get value or property from Stackview item when it popped in Qml?当 Stackview 项在 Qml 中弹出时,有什么方法可以从它获取值或属性吗? I want to get the edited name in 'EditProfile.qml' when it popped to 'Profile.qml' in the below project.当它弹出到下面项目中的“Profile.qml”时,我想在“EditProfile.qml”中获取编辑后的名称。

main.qml主文件

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

Profile.qml配置文件.qml

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

EditProfile.qml编辑配置文件.qml

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

After reading QT manual carefully, I found the answer.仔细阅读QT手册后,我找到了答案。 The push function returns the item which is pushed. push函数返回被推送的项目。 so:所以:

Profile.qml配置文件.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编辑配置文件.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()
            }
        }
    }
}     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM