Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved trying to access children property from a function defined in .js file

    QML and Qt Quick
    2
    4
    284
    Loading More Posts
    • 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.
    • ashajg
      ashajg last edited by

      hi guys

      I have a function in .js file. I am invoking that function on mouse click event and trying to change color of rectangle.

      .js function

      function check(iCompID)
      {
      console.log("--------->"+iCompID)
      parentID.children[iCompID].color="RED"
      }
      

      parentRectangle

      Rectangle
      {
      
          property string parentID: "mainrow"
      
          id:ventgrp
          width:800
          height:100
          color:"lightGray"
          anchors.bottom: parent.bottom
          Row
          {id:mainrow
          anchors.fill:parent
          }
      
      
          Component.onCompleted:
          {
          console.log("ventgrp created");
           ComponentCreator.createComponent();
          }
      
      
      }
      

      childRect

      import QtQuick 2.0
      import "qrc:/controller.js" as MoreSettingsCreation
      Rectangle
      {id:ventbtn
          width:200
          height:100
          color:"DarkGray"
          //anchors.left: parent.left
          border.color:"black"
            property int iCompID:0;
          Rectangle
          {
              id:vbutton
              height:40
              width:100
              anchors.centerIn: parent
              color:"lightgray"
              Text {anchors.centerIn: parent
                  id:vbuttontext
                  text: qsTr("Vent Mode")
              }
      
              MouseArea
              {
              anchors.fill:parent
              onClicked:{MoreSettingsCreation.createMoreSettingComp();
              MoreSettingsCreation.check(iCompID);
              }
              }
          }
      }
      

      ERROR:

      qrc:/controller.js:102: TypeError: Cannot read property '0' of undefined code 0
      

      problem is here it seems

      parentID.children[iCompID].color="RED"
      

      what is the problem ?

      1 Reply Last reply Reply Quote 0
      • ashajg
        ashajg last edited by

        one more thing guys if I am using parentID it is throwing this error but if I am using mainrow its working fine ... this line is faulty I guess property string parentID: "mainrow"

        any clue??

        Yashpal 1 Reply Last reply Reply Quote 0
        • Yashpal
          Yashpal @ashajg last edited by

          @ashajg Hi! I could see you have declared 'parentID' as string and trying to access it as QObject. Component id's are not string.

          Changing to the following, should work for you

          property string parentID: "mainrow"     --- > property alias parentID: mainrow
          
          1 Reply Last reply Reply Quote 2
          • ashajg
            ashajg last edited by

            thanks a lot @Yashpal

            1 Reply Last reply Reply Quote 0
            • First post
              Last post