Two dimension array in QML...
-
wrote on 1 Aug 2012, 07:27 last edited by
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.??
-
wrote on 1 Aug 2012, 09:45 last edited by
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
}}} -
wrote on 1 Aug 2012, 09:53 last edited by
Interesting... :) thanks...
-
wrote on 25 Nov 2012, 15:28 last edited by
I got an error
TypeError: Result of expression 'maintable[i]' [undefined] is not an object.my code is
@var i,j
maintable = new Array(3);
for(i=0;i<3;i++){
maintable[i]=new Array(3);
for(j=0;j<3;j++){
maintable[i][j]="."
}
}
@ -
wrote on 25 Nov 2012, 16:45 last edited by
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?
-
wrote on 25 Nov 2012, 16:51 last edited by
Thanks you favoritas37
the maintable is defined as variant
and Yes I use it in my code but that where the compiler points two,
-
wrote on 25 Nov 2012, 16:57 last edited by
Dear
Solved,
@var maintab
maintab = new Array(3);
for(i=0;i<3;i++){
maintab[i]=new Array(3);
for(j=0;j<3;j++){
maintab[i][j]="."
}
}
maintable=maintab@and thank you favoritas37
-
wrote on 26 Nov 2012, 00:20 last edited by
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. -
wrote on 26 Nov 2012, 09:32 last edited by
Thanks chris for the tip