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
    }}}



  • Interesting... :) thanks...



  • 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]="."
    }
    }
    @



  • 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?



  • Thanks you favoritas37

    the maintable is defined as variant

    and Yes I use it in my code but that where the compiler points two,



  • 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



  • 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.



  • Thanks chris for the tip


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.