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

Passing variables between QML files



  • Hi,
    I have a getName text field in AddToDb.qml:

     property alias nameParameter: getName.text
    
        AppTextField {
            id: getName
            width: backgrnd.width / 2.4
    
            height: dp(35)
            placeholderText: "Enter the Friend's name."
            placeholderColor: ST.placeHColor()
            showClearButton: true
            radius: 30
    
            //        textColor: ST.textColor()
            cursorColor: ST.cursorClr()
            font.pixelSize: 15
            anchors.top: id.bottom
            anchors.topMargin: 15
            anchors.left: id.left
            backgroundColor: ST.editBackground()
    
            selectionColor: ST.selectionColor()
            borderColor: ST.borderClr()
            borderWidth: 1
    
            onEditingFinished: {
                if (getName.text.length < 2) {
                    var popup = EP.createMsg(backgrnd, {
                                                 "text": "The Friend's name must be at least 2 characters long."
                                             })
                }
            }
        }
    

    I want to read the value of getName in AddToDBPage2.qml like this:

    var myName = AddToDB.nameParameter
    console.log("AddToDb myName: ", myName)
    

    I keep getting undefined in the console log instead of the name. What am I missing? Thank you for your help.



  • Hello @gabor53, is this AddToDBPage2.qml QML file parent of AddToDb.qml, is it sibling or are they totally unrelated? If they are parent/child or siblings then it should be easy to access via objectName.nameParameter. If they are unrelated, you will have to create some sort of variable accessible for both layouts and populate it from there.



  • @rrlopez
    They are unrelated.



  • @gabor53 you should be ok using QQmlContext: https://doc.qt.io/qt-5/qqmlcontext.html



  • @rrlopez
    I made some changes to the structure of the project. Both AddToDb.qml and AddToDBPage2.qml They are siblings and the parent is and other qml file called AddToDbMain.qml. In AddToDBPage2.qml when I typing

    var myName = AddToDB.nameParameter
    

    I can see the nameParameter property (after typing AddToDb. it pops up on the list), but the value is still undefined. What am I missing here?
    Thank you for your help.


Log in to reply