PDA

View Full Version : clear qlist inside qmap



franki
7th December 2013, 13:44
Hello,

If you have QMap of QList's like this


QMap<int,QList<int> > myMap;
myMap.insert(a,QList<int>())
myMap[a].append(b);
myMap[a].append(c);

and you clear QMap:


myMap.clear()

are elements of inner QList cleared, or you should iterate through QMap first and clear QLlist ?

best regards
Marek

anda_skoa
7th December 2013, 14:33
The lists are cleared as well.

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

Cheers,
_

franki
16th December 2013, 12:59
Hi,

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



struct SomeStruct {
....
}
QMap<int,QList<SomeStruct*> > myMap;

SomeStruct *s1=new SomeStruct;
SomeStruct *s2=new SomeStruct;

myMap[a].append[s1];
myMap[a].append[s2];
..............
myMap.clear();


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

wysota
16th December 2013, 13:17
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:


QList<SomeStruct*> list;
// ...

qDeleteAll(list);