Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Use a variable in another file
Qt 6.11 is out! See what's new in the release blog

Use a variable in another file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
14 Posts 4 Posters 5.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aktay
    wrote on last edited by
    #2

    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
    }
    
    1 Reply Last reply
    0
    • T Offline
      T Offline
      Titi01
      wrote on last edited by Titi01
      #3

      If I use

      /    Other{
              id:callOther
          }
      

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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Titi01
        wrote on last edited by
        #4

        @aktay ?

        A 1 Reply Last reply
        0
        • T Titi01

          @aktay ?

          A Offline
          A Offline
          aktay
          wrote on last edited by
          #5

          @Titi01

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

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Titi01
            wrote on last edited by
            #6

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

            A 1 Reply Last reply
            0
            • T Titi01

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

              A Offline
              A Offline
              aktay
              wrote on last edited by
              #7

              @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.

              1 Reply Last reply
              0
              • johngodJ Offline
                johngodJ Offline
                johngod
                wrote on last edited by
                #8

                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
                        }
                    }
                }
                
                A 1 Reply Last reply
                0
                • johngodJ johngod

                  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
                          }
                      }
                  }
                  
                  A Offline
                  A Offline
                  aktay
                  wrote on last edited by
                  #9

                  @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?

                  1 Reply Last reply
                  0
                  • johngodJ Offline
                    johngodJ Offline
                    johngod
                    wrote on last edited by
                    #10

                    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

                    A 1 Reply Last reply
                    0
                    • johngodJ johngod

                      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

                      A Offline
                      A Offline
                      aktay
                      wrote on last edited by
                      #11

                      @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.

                      1 Reply Last reply
                      0
                      • T Titi01

                        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...)
                        
                        YashpalY Offline
                        YashpalY Offline
                        Yashpal
                        wrote on last edited by
                        #12

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

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          Titi01
                          wrote on last edited by
                          #13

                          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

                          YashpalY 1 Reply Last reply
                          0
                          • T Titi01

                            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

                            YashpalY Offline
                            YashpalY Offline
                            Yashpal
                            wrote on last edited by
                            #14

                            @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.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved