Results 1 to 4 of 4

Thread: QList<QList > question

  1. #1
    Join Date
    Sep 2007
    Posts
    4
    Thanks
    2

    Default QList<QList > question

    Hello all,

    I'm trying to create a 2-dimensional QList of some structure.

    Here's the test code (contents of condition struct isn't important, both stateDWord and zone_id variables are int):
    Qt Code:
    1. taskData td2;
    2. QList<condition_struct*> *conditions2 = new QList<condition_struct*>;
    3. QList<QList<condition_struct*>* > *tasks2 = new QList<QList<condition_struct*>* >;
    4.  
    5. condition_struct *cnd2 = new condition_struct;
    6. cnd2->stateDWord = 1;
    7. cnd2->td_current.zone_id = 2;
    8. conditions2->push_back(cnd2);
    9. cnd2 = new condition_struct;
    10. cnd2->stateDWord = 3;
    11. cnd2->td_current.zone_id = 4;
    12. conditions2->push_back(cnd2);
    13.  
    14. tasks2->push_back(conditions2);
    15.  
    16. cnd2 = new condition_struct;
    17. cnd2->stateDWord = 5;
    18. cnd2->td_current.zone_id = 6;
    19. conditions2->push_back(cnd2);
    20. cnd2 = new condition_struct;
    21. cnd2->stateDWord = 7;
    22. cnd2->td_current.zone_id = 8;
    23. conditions2->push_back(cnd2);
    24. cnd2 = new condition_struct;
    25. cnd2->stateDWord = 9;
    26. cnd2->td_current.zone_id = 10;
    27. conditions2->push_back(cnd2);
    28.  
    29. tasks2->push_back(conditions2);
    30.  
    31. //...
    32.  
    33. condition_struct *cndCr = new condition_struct;
    34.  
    35. QList<condition_struct*> *conditionsCr = new QList<condition_struct*>;
    36.  
    37. printf("tasks2 vector size: %d\n\n",tasks2->size());
    38. for(QList<QList<condition_struct*>* >::iterator ti0=tasks2->begin();ti0!=tasks2->end();ti0++)
    39. {
    40. printf("conditions2 vector size: %d\n\n", (*ti0)->size());
    41. for(QList<condition_struct*>::iterator ci0=(*ti0)->begin();ci0!=(*ti0)->end();ci0++)
    42. {
    43. cndCr->stateDWord = (*ci0)->stateDWord;
    44. cndCr->td_current.zone_id = (*ci0)->td_current.zone_id;
    45. printf("stateDWord: %d, zone_id: %d\n",cndCr->stateDWord,cndCr->td_current.zone_id);
    46. (*ci0)->stateDWord = cndCr->stateDWord;
    47. (*ci0)->td_current.zone_id = cndCr->td_current.zone_id;
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 

    When running this code, I got the following output:
    Qt Code:
    1. tasks2 vector size: 2
    2.  
    3. conditions2 vector size: 5
    4.  
    5. stateDWord: 1, zone_id: 2
    6. stateDWord: 3, zone_id: 4
    7. stateDWord: 5, zone_id: 6
    8. stateDWord: 7, zone_id: 8
    9. stateDWord: 9, zone_id: 10
    10.  
    11. conditions2 vector size: 5
    12.  
    13. stateDWord: 1, zone_id: 2
    14. stateDWord: 3, zone_id: 4
    15. stateDWord: 5, zone_id: 6
    16. stateDWord: 7, zone_id: 8
    17. stateDWord: 9, zone_id: 10
    To copy to clipboard, switch view to plain text mode 
    instead of something like
    Qt Code:
    1. tasks2 vector size: 2
    2.  
    3. conditions2 vector size: 2
    4.  
    5. stateDWord: 1, zone_id: 2
    6. stateDWord: 3, zone_id: 4
    7.  
    8. conditions2 vector size: 3
    9.  
    10. stateDWord: 5, zone_id: 6
    11. stateDWord: 7, zone_id: 8
    12. stateDWord: 9, zone_id: 10
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? Thank you in advance for the answer.
    Last edited by SubV; 25th July 2008 at 12:34.

  2. #2
    Join Date
    Sep 2007
    Posts
    4
    Thanks
    2

    Default Re: QList<Qlist > question

    Already found the answer. I forget to initialize another copy of structure before pushing it to the list.
    Qt Code:
    1. taskData td2;
    2. QList<condition_struct*> *conditions2 = new QList<condition_struct*>;
    3. QList<QList<condition_struct*>* > *tasks2 = new QList<QList<condition_struct*>* >;
    4.  
    5. condition_struct *cnd2 = new condition_struct;
    6. cnd2->stateDWord = 1;
    7. cnd2->td_current.zone_id = 2;
    8. conditions2->push_back(cnd2);
    9. cnd2 = new condition_struct;
    10. cnd2->stateDWord = 3;
    11. cnd2->td_current.zone_id = 4;
    12. conditions2->push_back(cnd2);
    13.  
    14. tasks2->push_back(conditions2);
    15.  
    16. >>>>> conditions2 = new QList<condition_struct*>;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QList<QList > question

    I'd recommend not to allocate QLists on the heap in the first place because it's an implicitly shared class. Allocating it on the stack simplifies your code and makes it much less error prone.
    J-P Nurmi

  4. #4
    Join Date
    Sep 2007
    Posts
    4
    Thanks
    2

    Default Re: QList<QList > question

    jpn

    Thank you for your advice, I'll try this.

Similar Threads

  1. SQL Question
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 19:36
  2. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49
  3. Replies: 1
    Last Post: 15th March 2007, 20:45
  4. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38

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.