Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  121] [ 1]  / answers: 1 / hits: 24045  / 12 Years ago, mon, december 17, 2012, 12:00:00

I want to run through a list of QML components and choose one type:



for (var i = 0; i < controls.children.length; ++i) {
if ( typeof (controls.children[i].height) == QDeclarativeRectangle)
{
// do stuff
}
}


How does one accomplish this?


More From » qml

 Answers
11

You can't use typeof for this directly because it will always return you 'object' as a type of any QML element. There are several alternatives however that you could use. One is setting the objectName of the each element to its type and check that in your loop or define a property and check for that property. This will require a bit more work but you could create your qml element that has this property and than use it wherever you need it.
Here is a sample code:



Rectangle {
id: main
width: 300; height: 400

Rectangle {
id: testRect
objectName: rect
property int typeId: 1
}

Item {
id: testItem
objectName: other
}

Component.onCompleted: {
for(var i = 0; i < main.children.length; ++i)
{
if(main.children[i].objectName === rect)
{
console.log(got one rect)
}
else
{
console.log(non rect)
}
}
for(i = 0; i < main.children.length; ++i)
{
if(main.children[i].typeId === 1)
{
console.log(got one rect)
}
else
{
console.log(non rect)
}
}
}
}

[#81370] Saturday, December 15, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austonjuancarlosb

Total Points: 238
Total Questions: 89
Total Answers: 99

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;