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. trying to access children property from a function defined in .js file
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 512 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.
  • A Offline
    A Offline
    ashajg
    wrote on 11 Feb 2019, 09:40 last edited by
    #1

    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
    0
    • A Offline
      A Offline
      ashajg
      wrote on 11 Feb 2019, 10:01 last edited by
      #2

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

      Y 1 Reply Last reply 12 Feb 2019, 04:44
      0
      • A ashajg
        11 Feb 2019, 10:01

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

        Y Offline
        Y Offline
        Yashpal
        wrote on 12 Feb 2019, 04:44 last edited by
        #3

        @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
        2
        • A Offline
          A Offline
          ashajg
          wrote on 12 Feb 2019, 08:17 last edited by
          #4

          thanks a lot @Yashpal

          1 Reply Last reply
          0

          4/4

          12 Feb 2019, 08:17

          • Login

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