Results 1 to 4 of 4

Thread: clear qlist inside qmap

  1. #1
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default clear qlist inside qmap

    Hello,

    If you have QMap of QList's like this
    Qt Code:
    1. QMap<int,QList<int> > myMap;
    2. myMap.insert(a,QList<int>())
    3. myMap[a].append(b);
    4. myMap[a].append(c);
    To copy to clipboard, switch view to plain text mode 
    and you clear QMap:
    Qt Code:
    1. myMap.clear()
    To copy to clipboard, switch view to plain text mode 
    are elements of inner QList cleared, or you should iterate through QMap first and clear QLlist ?

    best regards
    Marek

  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: clear qlist inside qmap

    The lists are cleared as well.

    More precisely: each map element is deconstructed, the QList destructor clears the list.

    Cheers,
    _

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

    franki (16th December 2013)

  4. #3
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: clear qlist inside qmap

    Hi,

    What if elements of inner QList, or values of inner QMap are created with "new" operator like this

    Qt Code:
    1. struct SomeStruct {
    2. ....
    3. }
    4. QMap<int,QList<SomeStruct*> > myMap;
    5.  
    6. SomeStruct *s1=new SomeStruct;
    7. SomeStruct *s2=new SomeStruct;
    8.  
    9. myMap[a].append[s1];
    10. myMap[a].append[s2];
    11. ..............
    12. myMap.clear();
    To copy to clipboard, switch view to plain text mode 

    So in inner map i have in fact pointers to structure, or to other objects created with "new". Is there "delete" called for each object created with "new" ?

    best regards
    Marek

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: clear qlist inside qmap

    No, the list clears its contents and the content is pointers, not objects behind them. You can use qDeleteAll() to clear objects behind the pointers:

    Qt Code:
    1. QList<SomeStruct*> list;
    2. // ...
    3.  
    4. qDeleteAll(list);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    franki (16th December 2013)

Similar Threads

  1. pointer to nested QMap and QList
    By franki in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2013, 12:44
  2. QMap in QList | i can get value but i can`t set value
    By petrusPL in forum Qt Programming
    Replies: 4
    Last Post: 20th March 2013, 19:40
  3. QMap QList
    By dare5421 in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2011, 00:17
  4. Fastest way to clear a QList
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 10:26
  5. QMap in QList ?
    By npc in forum Newbie
    Replies: 2
    Last Post: 5th February 2007, 12:51

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.