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

Use a variable in another file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
14 Posts 4 Posters 3.4k Views
  • 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.
  • T Offline
    T Offline
    Titi01
    wrote on 27 Apr 2017, 13:42 last edited by
    #1

    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...)
    
    Y 1 Reply Last reply 2 May 2017, 04:50
    0
    • A Offline
      A Offline
      aktay
      wrote on 28 Apr 2017, 06:29 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 28 Apr 2017, 16:11 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 29 Apr 2017, 12:08 last edited by
          #4

          @aktay ?

          A 1 Reply Last reply 29 Apr 2017, 12:24
          0
          • T Titi01
            29 Apr 2017, 12:08

            @aktay ?

            A Offline
            A Offline
            aktay
            wrote on 29 Apr 2017, 12:24 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 29 Apr 2017, 13:35 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 1 May 2017, 08:53
              0
              • T Titi01
                29 Apr 2017, 13:35

                @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 1 May 2017, 08:53 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
                • J Offline
                  J Offline
                  johngod
                  wrote on 1 May 2017, 11:24 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 1 May 2017, 11:35
                  0
                  • J johngod
                    1 May 2017, 11:24

                    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 1 May 2017, 11:35 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
                    • J Offline
                      J Offline
                      johngod
                      wrote on 1 May 2017, 12:14 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 1 May 2017, 12:18
                      0
                      • J johngod
                        1 May 2017, 12:14

                        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 1 May 2017, 12:18 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
                          27 Apr 2017, 13:42

                          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...)
                          
                          Y Offline
                          Y Offline
                          Yashpal
                          wrote on 2 May 2017, 04:50 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 2 May 2017, 12:47 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

                            Y 1 Reply Last reply 8 May 2017, 04:15
                            0
                            • T Titi01
                              2 May 2017, 12:47

                              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

                              Y Offline
                              Y Offline
                              Yashpal
                              wrote on 8 May 2017, 04:15 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

                              3/14

                              28 Apr 2017, 16:11

                              topic:navigator.unread, 11
                              • Login

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