Results 1 to 5 of 5

Thread: How to insert a value of a Qlist inside of another Qlist

  1. #1
    Join Date
    Aug 2019
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Question 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
    Qt Code:
    1. struct Order{
    2. QList<OrderItem> items ;
    3. }
    4.  
    5. struct OrderItem{
    6. QString Address;
    7. }
    8.  
    9.  
    10. main.cpp
    11.  
    12. Ordergroup *ordergroup ;
    13. QList<Order> orderlist;
    14. for (int i = 0; i <3 ; ++i) {
    15. for (int j =0 ; j < 3 ; ++j){
    16. if ( i == j){
    17. auto& refgr = orderlist[i];
    18. refgr.items[j].set(OrderItem::Address, "21 Street") ;
    19. }
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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

    Qt Code:
    1. for (int i = 0; i < orderlist.count(); ++i) {
    To copy to clipboard, switch view to plain text mode 

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ofbrc (9th August 2019)

  4. #3
    Join Date
    Aug 2019
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

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

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    Last edited by d_stranz; 9th August 2019 at 18:03.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    ofbrc (6th September 2019)

  7. #5
    Join Date
    Aug 2019
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

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

Similar Threads

  1. clear qlist inside qmap
    By franki in forum Newbie
    Replies: 3
    Last Post: 16th December 2013, 14:17
  2. Replies: 3
    Last Post: 10th February 2012, 12:17
  3. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 17:04
  4. Replies: 4
    Last Post: 20th August 2010, 14:54
  5. QList inside a QList
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 1st July 2009, 12:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.