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



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



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



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


  • thanks a lot @Yashpal


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.