How to assign values to multidimensional QList
-
Hello.
I have defined
QList<Qlist<QList<Qvariant>>> lstempty 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 wasfor (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 -
@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? -
Hello.
I have defined
QList<Qlist<QList<Qvariant>>> lstempty 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 wasfor (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@emazed said in How to assign values to multidimensional QList:
but is not working
In what way it's not working?
-
@emazed said in How to assign values to multidimensional QList:
but is not working
In what way it's not working?
-
@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? -
Hello.
I have defined
QList<Qlist<QList<Qvariant>>> lstempty 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 wasfor (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@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. -
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
@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.QListdoes not offer a "set/re-size" method at Qt5 (so you have to append), though it does at Qt6.QVectorhas always had aresize(). You could useresize()to create elements/make it more like the C array. Although it's not an issue at Qt6, I would use aQVectorrather than aQListif i wanted to approximate the C. -
E emazed has marked this topic as solved on