Use a variable in another file



  • Hello,
    I have 2 files:
    -Button.qml
    -Other.qml

    I have 2 variable in button.qml (clikedX and clikedY)and I would like to retrieve the value of these variables in other.qml

    how should I do it?

    Button.qml:

    Rectangle {
        border.color: "black"
        radius:5
        width:60
        height:75
    
        property var clikedX;
        property var clikedY;
    
    
        MouseArea{
    
            anchors.fill:parent
    
        onClicked: {
           loader.source="other.qml"
           clikedX=parent.x
           clikedY=parent.y
    
             }
    

    other.qml:

    I would like here use the value of clikedX and ClikedY (so the value of parent.x and parent.y...)
    


  • Button.qml

    Window {
        visible: true
        width: 640
        height: 480
    
        property real clikedX;
        property real clikedY;
        Rectangle {
            border.color: "black"
            radius:5
            width:60
            height:75
            anchors.verticalCenter: parent.verticalCenter
            MouseArea{
                anchors.fill:parent
    
            onClicked: {
               clikedX=parent.x
               clikedY=parent.y
                callOther.otherX=clikedX
                callOther.otherY=clikedY
                 }}
    
    }
    
        Other{
            id:callOther
        }
    }
    
    

    Other.qml

    Item {
        property real otherX;
        property real otherY;
    
    //youre code here
    }
    


  • If I use

    /    Other{
            id:callOther
        }
    

    I have all the contents of Other.qml that appears in Button.qml
    @aktay



  • @aktay ?



  • @Titi01

    If you explain exactly what you want to do, maybe I can help. Otherwise there is no other idea.



  • @aktay So i have a variable in one file and i want use the value of this variable in an other file...



  • @Titi01

    I'm not sure but I do not think it's any other way. You can use C ++ for get data and send it back to QML.(emit data)

    If you find another way, I ask you to share.

    Good luck.



  • Maybe this is what you want :

    Button.qml

    Rectangle {
        border.color: "black"
        radius:5
        width:60
        height:75
    
        property var clikedX;
        property var clikedY;
    
        signal btnClick()
    
        MouseArea{
            anchors.fill: parent
            onClicked: {
                clikedX = mouseX
                clikedY = mouseY
                btnClick()
            }
        }
    }
    

    Other.qml

    Window {
        visible: true
        width: 640
        height: 480
        //title: qsTr("Hello World")
    
        Button {
            onBtnClick: {
                console.log(clikedX)
                console.log(clikedY)
                //do something with your clikedX/clikedY
            }
        }
    }
    


  • @johngod

    Isn't this similar to the following code?

    Button.qml

    
            onClicked: {
               clikedX=parent.x
               clikedY=parent.y
                callOther.otherX=clikedX
                callOther.otherY=clikedY
                 }}
    
    }
    
        Other{
            id:callOther
        }
    }
    

    Does not include all the content in Button.qml?



  • Hi @aktay
    It's similar but a bit different, by using the btnClick() signal, I'm decoupling the Buttom.qml from Other.qml, that way Button.qml works as a generic button that can be used anywhere



  • @johngod

    I know it. But I think @Titi01 does not want it.

    @Titi01 said in Use a variable in another file:

    If I use

    /    Other{
            id:callOther
        }
    

    I have all the contents of Other.qml that appears in Button.qml

    Let's see what happens.

    Thank you for your response.



  • @Titi01 Where do you want to create an object of Button.qml?



  • I create objects of "Button.qml" in the main.qml.
    But I would like that when I click on this button that clikedX and clikedY contain the value of parent.x and parent.y.
    Then after you can use the value of clikedX and clikedY in other.qml
    But I do not create a Button in Other.qml ... @Yashpal @johngod @aktay



  • @Titi01 Hopefully, you are creating an object of Other.qml in main.qml then, accessing clickedX and clickedY should be ease as objects of other.qml and button.qml are siblings. Other than this I'm unable to guess what you want to achieve.


Log in to reply