trying to access children property from a function defined in .js file
-
wrote on 11 Feb 2019, 09:40 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 ?
-
wrote on 11 Feb 2019, 10:01 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??
-
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??
wrote on 12 Feb 2019, 04:44 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
4/4