How to insert a value of a Qlist inside of another Qlist
I am trying to set a value inside a qlist of another qlist.
the code below gives me an error of ASSERT failure in QList<T> :: operator[] : "index out of range" .
main.hh
Code:
struct Order{
QList<OrderItem> items ;
}
struct OrderItem{
}
main.cpp
Ordergroup *ordergroup ;
QList<Order> orderlist;
for (int i = 0; i <3 ; ++i) {
for (int j =0 ; j < 3 ; ++j){
if ( i == j){
auto& refgr = orderlist[i];
refgr.items[j].set(OrderItem::Address, "21 Street") ;
}
}
}
I also tried the append but still it gives me an error of out of range.
If i set the the orderlist[i] into 1. it works but i need a set of arrays.
thanks in advance.
Re: How to insert a value of a Qlist inside of another Qlist
In line 17 you are trying to access an element of an empty list.
Since "i" is your index into the list you need to iterate over the size of the list, not some random hardcoded value
Code:
for (int i = 0; i < orderlist.count(); ++i) {
Same for "j" and the access to the other list
Or, if this is supposed to create the lists, append() instead of accessing by index
Cheers,
_
Re: How to insert a value of a Qlist inside of another Qlist
append is more appropriate , thanks for this .
but it still give me an error of out of range.
basically what i would like to do is append/copy the objects from QList<Order> to another QList<OrderGroup>having the same Struct .
Re: How to insert a value of a Qlist inside of another Qlist
In line 13 in your original code, you declare a QList named "orderlist". This is an empty list. In line 17, you try to access the item at index "i" in this list. Because the list is empty, there is no item at -any- index (including zero), so you get an out of range error.
Did you make a mistake and declare this "orderlist" as a local variable, and thereby maybe hide a member variable in your class with the same name that actually does have contents?
In any case, you should also follow anda_skoa's suggestion to use "orderlist.count()" as the terminator for your loop variable instead of a hard-coded "3".
Also, your loops over i and j don't make a lot of sense. You are executing the innermost loop 9 times (3 times 3 for i * j), but you are only doing something if i == j. You can get rid of the inner loop entirely, and move its code into the loop over i. Replace j with i and you get the same result.
Re: How to insert a value of a Qlist inside of another Qlist
I resolved it by declaring append for both QList<Order> and QList<OrderItem>