Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Two dimension array in QML...

Two dimension array in QML...

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 4 Posters 12.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aekam
    wrote on last edited by
    #1

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

    If you take care of inches, you won't have to worry about miles... :)

    1 Reply Last reply
    0
    • F Offline
      F Offline
      favoritas37
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aekam
        wrote on last edited by
        #3

        Interesting... :) thanks...

        If you take care of inches, you won't have to worry about miles... :)

        1 Reply Last reply
        0
        • B Offline
          B Offline
          blakxu00
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • F Offline
            F Offline
            favoritas37
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • B Offline
              B Offline
              blakxu00
              wrote on last edited by
              #6

              Thanks you favoritas37

              the maintable is defined as variant

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

              1 Reply Last reply
              0
              • B Offline
                B Offline
                blakxu00
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  chrisadams
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    blakxu00
                    wrote on last edited by
                    #9

                    Thanks chris for the tip

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved