Solved how to find children length of a window
-
Hi guys
I want to find length of children in a window
In my main.qml I am writing a function
function getGroupRef() { var length=parent.children.length console.log("MaInWiNdOw children length---->"+length) }
Main.qml
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick 2.9 import "qrc:/MasterController.js" as Controll Window { id:mainWindow visible: true width: 800 height: 600 title: qsTr("Hello World") Component.onCompleted: { console.log ("triggered") Controll.createGroup(); } function getGroupRef() { var length=mainWindow.children.length console.log("MaInWiNdOw children length---->"+length) } MouseArea { anchors.fill:parent onClicked:getGroupRef() } }
MasterController.js
var sprite var groupComponent; var groupComponentTwo; var ventGroupContainer; var barButtonContainer; var myList=[]; var myCompNames=["ventbutmod","qkeyWidget1","qkeyWidget2","qkeyWidget3","qkeyWidget4","moreset","buttonBar0","buttonBar1","buttonBar2"]; function createGroup() { console.log("inside Create Group") groupComponent = Qt.createComponent("qrc:/VentGroup.qml"); groupComponentTwo=Qt.createComponent("qrc:/ButtonBarGroup.qml"); if(groupComponent.status === Component.Ready && groupComponentTwo.status === Component.Ready){ finishGroupCreation();} else { groupComponent.statusChanged.connect(finishGroupCreation); groupComponentTwo.statusChanged.connect(finishGroupCreation) } } function finishGroupCreation() { console.log("inside finishgroupcreation") barButtonContainer=groupComponentTwo.createObject(mainWindow) ventGroupContainer=groupComponent.createObject(mainWindow) }
ERROR: qrc:/main.qml:47: TypeError: Cannot read property 'length' of undefined
In this program I am creating two components using .js . when I am trying to fetch children length of mainWindow which should be 2(ventgroup and buttonBargroup). I am getting error .
How can I solve this? -
@ashajg
Window
is not a visual QML item neither does it have achildren
property thus the error.I haven't tested the following but i think it's closer to what you try to achieve:
var length=mainWindow.contentItem.children.length
-
@raven-worx said in how to find children length of a window:
var length=mainWindow.contentItem.children.length
hi
this line var length=mainWindow.contentItem.children.length is giving me answer as 3.
What is contentItem ?Actually I am trying to fetch Objectnames of Ventgroup.qml and ButtonBargroup.qml
something like this
function getReference(objectName) { var length = mainWindow.children.length console.log("length of children is "+length) for(var i=0;i<length;i++) { if(objectName===mainWindow.children[i].objectName) { console.log("FOUND"); return mainWindow.children[i]; } } }
basically I am trying to fetch mainWindow.children[i]; so that I can manuplate properties of this group from my .js
any idea? -
@ashajg said in how to find children length of a window:
What is contentItem ?
the contentItem is the visual item holding the child items in the window
-
ok
I was able to get it like this
```
function getGroupRef(groupName) { var length=mainWindow.contentItem.children.length console.log("MaInWiNdOw children length---->"+length) for(var i=0;i<length;i++) { if(mainWindow.contentItem.children[i].objectName===groupName) { console.log("found at :"+i) return mainWindow.contentItem.children[i]; } } // console.log("MaInWiNdOw children name---->"+mainWindow.contentItem.children[2].objectName) }
-
@ashajg
great, that it's now working for you.Don't forget to set the topic to solved, via topic tools.
-
yes
Thank you!!!