Two dimension array in QML...
-
Hello,
I was wondering about how to use two dimension arrays in QML.
One way i found is,
@
property variant myArray: [[0, 1, 2], [7, 8, 9]]
@now to print it,
@
// prints 0
console.log ("array element is = " + myArray[0][0])
// prints 9
console.log ("array element is = " + myArray[1][2])
@is this right way to create and access it.??
-
Yeah it is quite fine.
A more dynamic way would be:
{{{
var 2dArray = new Array(5);
2dArray[ 0 ] = newArray(5);
2dArray[ 1 ] = newArray(5);
//and on and on as many as you want//Then print just like the code you posted
}}} -
I will suppose you mean:
@
var i,j;
var maintable = new Array(3);
for(i=0;i<3;i++){
maintable[i]=new Array(3);
for(j=0;j<3;j++){
maintable[i][j]="."
}
}
@This code looks and works fine.
Do you use the maintable somewhere else in your code?
-
Please be aware that when you use a variant property to store your array, it is actually being converted to a QVariant. Accessing it from the QVariant property will incur some conversion overhead. In Qt5/QtQuick2, you should use the "var" property type instead - but this property type is not available in Qt4/QtQuick1.
Cheers,
Chris.