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. Change component's child propery from javascript file
Forum Update on Monday, May 27th 2025

Change component's child propery from javascript file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
21 Posts 3 Posters 3.5k 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.
  • B Offline
    B Offline
    behruz montazeri
    wrote on 31 Aug 2018, 10:45 last edited by behruz montazeri 9 Jan 2018, 04:38
    #1

    I have this error :

    qrc:/ColorFunc.js:3: ReferenceError: rectId is not defined
    

    My qml file is Rect50.qml :

    import "ColorFunc.js" as MyColor
    ............
    
    onPressed: MyColor.changeColor(colorM)
    

    My extrnal javascript file ColorFunc.js :

    function changeColor(param1) {
    
            rectId.color = param1;
    }
    
    
    D 1 Reply Last reply 1 Sept 2018, 04:15
    0
    • R Offline
      R Offline
      rweickelt
      wrote on 31 Aug 2018, 12:46 last edited by
      #2

      Hard to see what is wrong in your code. If you could post a minimal, but still complete example, then it is more likely that you will get an answer.

      B 1 Reply Last reply 31 Aug 2018, 15:42
      0
      • R rweickelt
        31 Aug 2018, 12:46

        Hard to see what is wrong in your code. If you could post a minimal, but still complete example, then it is more likely that you will get an answer.

        B Offline
        B Offline
        behruz montazeri
        wrote on 31 Aug 2018, 15:42 last edited by
        #3

        @rweickelt

        Component{
                    id:mComponent
                   Item {
                        width:parent.width
                        height: rectId.height
                        Rectangle{
                            id:rectId
                            width: parent.width
        
        D 1 Reply Last reply 31 Aug 2018, 19:17
        0
        • B behruz montazeri
          31 Aug 2018, 15:42

          @rweickelt

          Component{
                      id:mComponent
                     Item {
                          width:parent.width
                          height: rectId.height
                          Rectangle{
                              id:rectId
                              width: parent.width
          
          D Offline
          D Offline
          Diracsbracket
          wrote on 31 Aug 2018, 19:17 last edited by
          #4

          @behruz-montazeri
          Is that supposed to be complete code? Minimal effort from your side will result in minimal efforts from others trying to help.
          Cheers.

          B 1 Reply Last reply 1 Sept 2018, 03:55
          0
          • D Diracsbracket
            31 Aug 2018, 19:17

            @behruz-montazeri
            Is that supposed to be complete code? Minimal effort from your side will result in minimal efforts from others trying to help.
            Cheers.

            B Offline
            B Offline
            behruz montazeri
            wrote on 1 Sept 2018, 03:55 last edited by behruz montazeri 9 Jan 2018, 03:59
            #5

            @Diracsbracket

            The complete qml file :

            import QtQuick 2.0
            import QtQuick.Controls 2.1
            import QtQuick.Window 2.2
            import "ColorFunc.js" as MyColor
            
            Item {
                visible: true
                width: 640
                height: 480
            
            
            
                ListModel{
                    id:mModel
            
                    ListElement{
                        name:"chery"
                        number:"1850"
                        colorM:"khaki"
                        msource:"qrc:/chery.png"
                    }
                    ListElement{
                        name:"banana"
                        number:"15829"
                        colorM:"yellowgreen"
                        msource:"qrc:/banana.png"
                    }
                    ListElement{
                        name:"grep"
                        number:"1500"
                        colorM:"lightblue"
                        msource:"qrc:/grep.png"
            
                    }
                    ListElement{
                        name:"chery"
                        number:"1850"
                        colorM:"khaki"
                        msource:"qrc:/chery.png"
                    }
                    ListElement{
                        name:"banana"
                        number:"15829"
                        colorM:"yellowgreen"
                        msource:"qrc:/banana.png"
                    }
                    ListElement{
                        name:"grep"
                        number:"1500"
                        colorM:"lightblue"
                        msource:"qrc:/grep.png"
            
                    }
            
                }
                ListView{
                    anchors.fill: parent
                    model: mModel
                    delegate: mComponent
                    highlight: Rectangle{
                        width: parent.width
                        color: "red"
                    }
            
            
            
                    Component{
                        id:mComponent
                       Item {
                            width:parent.width
                            height: rectId.height
                            Rectangle{
                                id:rectId
                                width: parent.width
                                height: 80
                                border.width: 6
                                border.color: "black"
            
                                property color mColor: "firebrick"
            
                                Text {
             
                                    anchors.verticalCenter: parent.verticalCenter
                                    x:30
            
                                    id: txt1
                                    text: name
                                    font.pixelSize: 25
                                }
                                Image {
                                    anchors.centerIn: parent
                                    width:70
                                    height: width
                                    id: mPic
                                    source: msource
                                }
                                ItemDelegate{
                                    anchors.fill: parent
                                    onClicked: console.log(name)
                                    onPressed: MyColor.changeColor(colorM)
            
                                }
                            }
                            property alias rect5: rectId.color
                        }
                    }
                }
            
            }
            
            1 Reply Last reply
            0
            • B behruz montazeri
              31 Aug 2018, 10:45

              I have this error :

              qrc:/ColorFunc.js:3: ReferenceError: rectId is not defined
              

              My qml file is Rect50.qml :

              import "ColorFunc.js" as MyColor
              ............
              
              onPressed: MyColor.changeColor(colorM)
              

              My extrnal javascript file ColorFunc.js :

              function changeColor(param1) {
              
                      rectId.color = param1;
              }
              
              
              D Offline
              D Offline
              Diracsbracket
              wrote on 1 Sept 2018, 04:15 last edited by Diracsbracket 9 Jan 2018, 04:16
              #6

              Hi @behruz-montazeri
              With the full code, that was so much easier to understand and to test ...

              Change the following:

              onPressed: MyColor.changeColor(rectId, colorM)
              

              and

              function changeColor(rectId, param1) {
                  rectId.color = param1;
              }
              

              The reason your code does not work is that the ID you define inside a Component's Item has no meaning in the context after it is created.

              B 1 Reply Last reply 1 Sept 2018, 04:22
              1
              • D Diracsbracket
                1 Sept 2018, 04:15

                Hi @behruz-montazeri
                With the full code, that was so much easier to understand and to test ...

                Change the following:

                onPressed: MyColor.changeColor(rectId, colorM)
                

                and

                function changeColor(rectId, param1) {
                    rectId.color = param1;
                }
                

                The reason your code does not work is that the ID you define inside a Component's Item has no meaning in the context after it is created.

                B Offline
                B Offline
                behruz montazeri
                wrote on 1 Sept 2018, 04:22 last edited by
                #7

                @Diracsbracket
                Thanks it works. I want to make a same game ,when player click on each list element check with last click
                and if it's same fruit as current click do something. Do you know what's the name of this kinda game ?
                is there any example to learn from ?

                D 1 Reply Last reply 1 Sept 2018, 04:39
                0
                • B behruz montazeri
                  1 Sept 2018, 04:22

                  @Diracsbracket
                  Thanks it works. I want to make a same game ,when player click on each list element check with last click
                  and if it's same fruit as current click do something. Do you know what's the name of this kinda game ?
                  is there any example to learn from ?

                  D Offline
                  D Offline
                  Diracsbracket
                  wrote on 1 Sept 2018, 04:39 last edited by
                  #8

                  @behruz-montazeri
                  You mean a Matching Game like this one:
                  https://www.youtube.com/watch?v=QsLG-1CbfTQ
                  I think you are well on your way to succeed.

                  However, the use of an extra ItemDelegate in your Component definition is somewhat redundant. You could it improve it by doing e.g.:

                              Component{
                                  id:mComponent
                  
                                 MouseArea {
                                      id: mouseArea
                                      width:parent.width
                                      height: rectId.height
                  
                                      Rectangle{
                                          id:rectId
                                          width: parent.width
                                          height: 80
                                          border.width: 6
                                          border.color: "black"
                  
                                          color: mouseArea.containsPress ? colorM : "firebrick"
                  
                                          Text {
                  
                                              anchors.verticalCenter: parent.verticalCenter
                                              x:30
                  
                                              id: txt1
                                              text: name
                                              font.pixelSize: 25
                                          }
                                          Image {
                                              anchors.centerIn: parent
                                              width:70
                                              height: width
                                              id: mPic
                                              source: msource
                                          }
                                      }
                                  }
                              }
                  
                  1 Reply Last reply
                  1
                  • B Offline
                    B Offline
                    behruz montazeri
                    wrote on 1 Sept 2018, 08:29 last edited by
                    #9

                    How to make a variable static inside java script each time i click lastColor get the same value.
                    How can i prevent that ?

                    function changeColor(rectId, param1) {
                    
                        var   lastColor ="#ffffff"
                        console.log("last Color: "+lastColor)
                        console.log("ColorM: "+param1)
                    
                        if(lastColor === param1 ){
                            console.log("Second");
                             rectId.color = param1
                            lastColor = "#ffffff";
                            return
                        }
                        if(lastColor == "#ffffff"){
                            rectId.color = param1
                            lastColor = param1;
                    
                            console.log("First");
                    
                        }
                    
                        else console.log(lastColor)
                    
                    }
                    
                    D 1 Reply Last reply 1 Sept 2018, 08:35
                    0
                    • B behruz montazeri
                      1 Sept 2018, 08:29

                      How to make a variable static inside java script each time i click lastColor get the same value.
                      How can i prevent that ?

                      function changeColor(rectId, param1) {
                      
                          var   lastColor ="#ffffff"
                          console.log("last Color: "+lastColor)
                          console.log("ColorM: "+param1)
                      
                          if(lastColor === param1 ){
                              console.log("Second");
                               rectId.color = param1
                              lastColor = "#ffffff";
                              return
                          }
                          if(lastColor == "#ffffff"){
                              rectId.color = param1
                              lastColor = param1;
                      
                              console.log("First");
                      
                          }
                      
                          else console.log(lastColor)
                      
                      }
                      
                      D Offline
                      D Offline
                      Diracsbracket
                      wrote on 1 Sept 2018, 08:35 last edited by
                      #10

                      @behruz-montazeri
                      Do a Google search for that: JavaScript + static local variable.
                      e.g. https://stackoverflow.com/questions/1535631/static-variables-in-javascript

                      B 1 Reply Last reply 2 Sept 2018, 06:59
                      2
                      • D Diracsbracket
                        1 Sept 2018, 08:35

                        @behruz-montazeri
                        Do a Google search for that: JavaScript + static local variable.
                        e.g. https://stackoverflow.com/questions/1535631/static-variables-in-javascript

                        B Offline
                        B Offline
                        behruz montazeri
                        wrote on 2 Sept 2018, 06:59 last edited by
                        #11

                        @Diracsbracket
                        I did one. I changed my function. But here's another problem it work for only one qml file and if we call it from another qml file counter is Zero.

                        function changeColor(rectId, param1) {
                            if( typeof changeColor.counter == 'undefined' ) {
                                changeColor.counter  = 0;
                            }
                            changeColor.counter++
                        }
                        
                        D 1 Reply Last reply 2 Sept 2018, 07:36
                        0
                        • B behruz montazeri
                          2 Sept 2018, 06:59

                          @Diracsbracket
                          I did one. I changed my function. But here's another problem it work for only one qml file and if we call it from another qml file counter is Zero.

                          function changeColor(rectId, param1) {
                              if( typeof changeColor.counter == 'undefined' ) {
                                  changeColor.counter  = 0;
                              }
                              changeColor.counter++
                          }
                          
                          D Offline
                          D Offline
                          Diracsbracket
                          wrote on 2 Sept 2018, 07:36 last edited by
                          #12

                          @behruz-montazeri
                          Then use a global JS variable.

                          B 1 Reply Last reply 2 Sept 2018, 09:12
                          1
                          • D Diracsbracket
                            2 Sept 2018, 07:36

                            @behruz-montazeri
                            Then use a global JS variable.

                            B Offline
                            B Offline
                            behruz montazeri
                            wrote on 2 Sept 2018, 09:12 last edited by
                            #13

                            @Diracsbracket
                            Yes that's the case.

                             property int counter: 0
                            
                            D 1 Reply Last reply 2 Sept 2018, 09:55
                            0
                            • B behruz montazeri
                              2 Sept 2018, 09:12

                              @Diracsbracket
                              Yes that's the case.

                               property int counter: 0
                              
                              D Offline
                              D Offline
                              Diracsbracket
                              wrote on 2 Sept 2018, 09:55 last edited by
                              #14

                              @behruz-montazeri
                              that's not a JS variable. That's a QML object property.

                              B 1 Reply Last reply 2 Sept 2018, 10:01
                              0
                              • D Diracsbracket
                                2 Sept 2018, 09:55

                                @behruz-montazeri
                                that's not a JS variable. That's a QML object property.

                                B Offline
                                B Offline
                                behruz montazeri
                                wrote on 2 Sept 2018, 10:01 last edited by
                                #15

                                @Diracsbracket
                                Where can i declare a javascript global variable ?

                                D 1 Reply Last reply 2 Sept 2018, 10:03
                                0
                                • B behruz montazeri
                                  2 Sept 2018, 10:01

                                  @Diracsbracket
                                  Where can i declare a javascript global variable ?

                                  D Offline
                                  D Offline
                                  Diracsbracket
                                  wrote on 2 Sept 2018, 10:03 last edited by Diracsbracket 9 Feb 2018, 10:05
                                  #16

                                  @behruz-montazeri said in Change component's child propery from javascript file:

                                  @Diracsbracket
                                  Where can i declare a javascript global variable ?

                                  In a javascript file. Otherwise create a property at a top-level QML object, e.g. your main window, which exists for the duration of your app.

                                  B 1 Reply Last reply 2 Sept 2018, 10:12
                                  0
                                  • D Diracsbracket
                                    2 Sept 2018, 10:03

                                    @behruz-montazeri said in Change component's child propery from javascript file:

                                    @Diracsbracket
                                    Where can i declare a javascript global variable ?

                                    In a javascript file. Otherwise create a property at a top-level QML object, e.g. your main window, which exists for the duration of your app.

                                    B Offline
                                    B Offline
                                    behruz montazeri
                                    wrote on 2 Sept 2018, 10:12 last edited by behruz montazeri 9 Feb 2018, 10:13
                                    #17

                                    @Diracsbracket
                                    I guess creating property make seance in my situation. making a Javascript file variable doesn't keep track of increasing despite the several calls from different QML files.

                                    D 1 Reply Last reply 2 Sept 2018, 10:18
                                    0
                                    • B behruz montazeri
                                      2 Sept 2018, 10:12

                                      @Diracsbracket
                                      I guess creating property make seance in my situation. making a Javascript file variable doesn't keep track of increasing despite the several calls from different QML files.

                                      D Offline
                                      D Offline
                                      Diracsbracket
                                      wrote on 2 Sept 2018, 10:18 last edited by Diracsbracket 9 Feb 2018, 10:19
                                      #18

                                      @behruz-montazeri said in Change component's child propery from javascript file:

                                      I guess creating property make seance in my situation.

                                      That's usually the best way indeed.

                                      making a Javascript file variable doesn't keep track of increasing despite the several calls from different QML files.

                                      You can do it, if you really need it:
                                      http://doc.qt.io/qt-5/qtqml-javascript-resources.html#shared-javascript-resources-libraries

                                      B 1 Reply Last reply 2 Sept 2018, 10:59
                                      0
                                      • D Diracsbracket
                                        2 Sept 2018, 10:18

                                        @behruz-montazeri said in Change component's child propery from javascript file:

                                        I guess creating property make seance in my situation.

                                        That's usually the best way indeed.

                                        making a Javascript file variable doesn't keep track of increasing despite the several calls from different QML files.

                                        You can do it, if you really need it:
                                        http://doc.qt.io/qt-5/qtqml-javascript-resources.html#shared-javascript-resources-libraries

                                        B Offline
                                        B Offline
                                        behruz montazeri
                                        wrote on 2 Sept 2018, 10:59 last edited by behruz montazeri 9 Feb 2018, 11:00
                                        #19

                                        @Diracsbracket

                                        I have a list view i want to change the color of all rectangle inside of component with Javascript.

                                            ListView{
                                                id:fruitList
                                                anchors.fill: parent
                                                model: mModel
                                                delegate: mComponent
                                                highlight: Rectangle{
                                                    width: parent.width
                                                    color: "red"
                                                }
                                                Component{
                                                    id:mComponent
                                                   Item {
                                                        width:parent.width
                                                        height: rectId.height
                                                        Rectangle{
                                                            id:rectId
                                                            width: parent.width
                                                            height: 80
                                                            border.width: 6
                                                            border.color: "black"
                                        
                                                            Image {
                                                                anchors.centerIn: parent
                                                                width:70
                                                                height: width
                                                                id: mPic
                                                                source: msource
                                                            }
                                                            ItemDelegate{
                                                                anchors.fill: parent
                                                                onClicked: {
                                                                    MyColor.changeColor(rectId,colorM)
                                                                    mText =  MyColor.textReturn("page1")
                                                               } 
                                        
                                                            }
                                                        }
                                        
                                                    }
                                                }
                                        

                                        in Javascript :

                                        function resetList()
                                        {
                                            for (var entry = 0; entry < fruitList.count; ++entry) {
                                                
                                                 // rectId.color = "white'
                                            }
                                        }
                                        
                                        D 1 Reply Last reply 2 Sept 2018, 13:18
                                        0
                                        • B behruz montazeri
                                          2 Sept 2018, 10:59

                                          @Diracsbracket

                                          I have a list view i want to change the color of all rectangle inside of component with Javascript.

                                              ListView{
                                                  id:fruitList
                                                  anchors.fill: parent
                                                  model: mModel
                                                  delegate: mComponent
                                                  highlight: Rectangle{
                                                      width: parent.width
                                                      color: "red"
                                                  }
                                                  Component{
                                                      id:mComponent
                                                     Item {
                                                          width:parent.width
                                                          height: rectId.height
                                                          Rectangle{
                                                              id:rectId
                                                              width: parent.width
                                                              height: 80
                                                              border.width: 6
                                                              border.color: "black"
                                          
                                                              Image {
                                                                  anchors.centerIn: parent
                                                                  width:70
                                                                  height: width
                                                                  id: mPic
                                                                  source: msource
                                                              }
                                                              ItemDelegate{
                                                                  anchors.fill: parent
                                                                  onClicked: {
                                                                      MyColor.changeColor(rectId,colorM)
                                                                      mText =  MyColor.textReturn("page1")
                                                                 } 
                                          
                                                              }
                                                          }
                                          
                                                      }
                                                  }
                                          

                                          in Javascript :

                                          function resetList()
                                          {
                                              for (var entry = 0; entry < fruitList.count; ++entry) {
                                                  
                                                   // rectId.color = "white'
                                              }
                                          }
                                          
                                          D Offline
                                          D Offline
                                          Diracsbracket
                                          wrote on 2 Sept 2018, 13:18 last edited by
                                          #20

                                          @behruz-montazeri
                                          I think this thread is closed. Thanks.

                                          B 1 Reply Last reply 2 Sept 2018, 13:20
                                          0

                                          6/21

                                          1 Sept 2018, 04:15

                                          topic:navigator.unread, 15
                                          • Login

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