Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to assign values to multidimensional QList
Qt 6.11 is out! See what's new in the release blog

How to assign values to multidimensional QList

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 779 Views 2 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.
  • E Offline
    E Offline
    emazed
    wrote on last edited by
    #1

    Hello.

    I have defined QList<Qlist<QList<Qvariant>>> lst empty container, and i want to create a structure similiar to a c array, as v[10][5][8]. How can i put values into QLists?? my idea was

    for (i=0; i< 10; i++) {
     for (j=0; j<5; j++) {
      for (k=0;k<8; k++) {
       lst[i][j][k] = my_value;
      }
     }
    }
    

    but is not working, how can i solve that?

    Kind Regards
    emazed

    jsulmJ Pl45m4P 2 Replies Last reply
    0
    • E emazed

      @jsulm i receive a Signal Abort, and the message is index out of range.
      I understand that the lst is empty, what s the proper way to do it?

      E Offline
      E Offline
      emazed
      wrote on last edited by
      #4

      ok Qt Help says:
      T &QList::operator[](int i)

      Returns the item at index position i as a modifiable reference. i must be a valid index position in the list (i.e., 0 <= i < size()).

      so the element must exists before i can copy a value in it. ok

      JonBJ 1 Reply Last reply
      0
      • E emazed

        Hello.

        I have defined QList<Qlist<QList<Qvariant>>> lst empty container, and i want to create a structure similiar to a c array, as v[10][5][8]. How can i put values into QLists?? my idea was

        for (i=0; i< 10; i++) {
         for (j=0; j<5; j++) {
          for (k=0;k<8; k++) {
           lst[i][j][k] = my_value;
          }
         }
        }
        

        but is not working, how can i solve that?

        Kind Regards
        emazed

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @emazed said in How to assign values to multidimensional QList:

        but is not working

        In what way it's not working?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        E 1 Reply Last reply
        0
        • jsulmJ jsulm

          @emazed said in How to assign values to multidimensional QList:

          but is not working

          In what way it's not working?

          E Offline
          E Offline
          emazed
          wrote on last edited by
          #3

          @jsulm i receive a Signal Abort, and the message is index out of range.
          I understand that the lst is empty, what s the proper way to do it?

          E 1 Reply Last reply
          0
          • E emazed

            @jsulm i receive a Signal Abort, and the message is index out of range.
            I understand that the lst is empty, what s the proper way to do it?

            E Offline
            E Offline
            emazed
            wrote on last edited by
            #4

            ok Qt Help says:
            T &QList::operator[](int i)

            Returns the item at index position i as a modifiable reference. i must be a valid index position in the list (i.e., 0 <= i < size()).

            so the element must exists before i can copy a value in it. ok

            JonBJ 1 Reply Last reply
            0
            • E emazed

              Hello.

              I have defined QList<Qlist<QList<Qvariant>>> lst empty container, and i want to create a structure similiar to a c array, as v[10][5][8]. How can i put values into QLists?? my idea was

              for (i=0; i< 10; i++) {
               for (j=0; j<5; j++) {
                for (k=0;k<8; k++) {
                 lst[i][j][k] = my_value;
                }
               }
              }
              

              but is not working, how can i solve that?

              Kind Regards
              emazed

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #5

              @emazed said in How to assign values to multidimensional QList:

              i want to create a structure similiar to a c array, as v[10][5][8]

              Just a note:

              Note: QVector and QVarLengthArray both guarantee C-compatible array layout.
              QList does not. This might be important if your application must interface with a C API.
              

              (https://doc.qt.io/qt-5/qlist.html#details)


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • E emazed

                ok Qt Help says:
                T &QList::operator[](int i)

                Returns the item at index position i as a modifiable reference. i must be a valid index position in the list (i.e., 0 <= i < size()).

                so the element must exists before i can copy a value in it. ok

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @emazed said in How to assign values to multidimensional QList:

                so the element must exists before i can copy a value in it. ok

                Yes you must create the elements for each dimension before you can address them. A C array v[10][5][8] is pre-sized to hold all the elements. QList does not offer a "set/re-size" method at Qt5 (so you have to append), though it does at Qt6. QVector has always had a resize(). You could use resize() to create elements/make it more like the C array. Although it's not an issue at Qt6, I would use a QVector rather than a QList if i wanted to approximate the C.

                1 Reply Last reply
                1
                • E emazed has marked this topic as solved on

                • Login

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