Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set textfield data from one page to another page in qml



  • Hello,
    I am learning qt. I want to set textfield data of second page from main page. When I set text to textfield of main page, the textfield of second page is also have to set. I have following code structure

    main.qml
    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4

    ApplicationWindow {
    id: root
    visible: true
    width : 400
    height: 300

    StackView {
        id: stack
        initialItem: view
    
        Component {
            id: view
    
            MouseArea {
                Text {
                    text: stack.depth
                    anchors.centerIn: parent
                }
                onClicked: stack.push(view)
            }
        }
    }
    Button {
        id: button
        x: 86
        y: 95
        width: 114
        height: 54
        text: qsTr("Button")
        onClicked: {
           console.debug("New Page")
              stack.pop(StackView.Immediate)
           stack.push (Qt.resolvedUrl("Newalt.qml"))
        }
    }
    TextField{
        id: setText
        text: "ASK"
    }
    

    }

    Newalt.qml
    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4

    Item {
    Label {
    id: label
    x: 249
    y: 166
    width: 138
    height: 58
    text: qsTr("New page")
    font.pointSize: 11
    fontSizeMode: Text.VerticalFit
    verticalAlignment: Text.AlignVCenter

    }
    
    TextField{
    id: getText
    text: ""
    }
    

    }



  • @Srsirsat

    I don't know if it what you want but, put a property in your Newalt.qml then when you do the stack.push add the property with the value of the setText.text see the following example :

    main.qml :

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    
    ApplicationWindow {
        id: root
        visible: true
        width : 400
        height: 300
    
        StackView {
            id: stack
            initialItem: view
    
            Component {
                id: view
    
                MouseArea {
                    Text {
                        text: stack.depth
                        anchors.centerIn: parent
                    }
                    onClicked: stack.push(view)
                }
            }
        }
        Button {
            id: button
            x: 86
            y: 95
            width: 114
            height: 54
            text: qsTr("Button")
            onClicked: {
                console.debug("New Page")
                stack.pop(StackView.Immediate)
                stack.push ({item: Qt.resolvedUrl("Newalt.qml"), properties: {textToSet: setText.text}}) //Push with the property value
            }
        }
        TextField{
            id: setText
            text: "ASK"
        }
    
    }
    
    

    Newalt.qml :

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    
    Item {
        property string textToSet: "New page"
        Label {
            id: label
            x: 249
            y: 166
            width: 138
            height: 58
            text: qsTr("New Page")
            font.pointSize: 11
            fontSizeMode: Text.VerticalFit
            verticalAlignment: Text.AlignVCenter
    
        }
    
        TextField{
            id: getText
            text: textToSet
            anchors.top: label.bottom
            anchors.left: label.left
        }
    
    }
    
    
    

Log in to reply