PDA

View Full Version : deleting all elements in a qlist



reshma
12th March 2009, 13:47
hello,

can anyone help me out in deleting all the elements in a qlist??

i want to clear a qlist totally so that new data can be reloaded...

navi1084
12th March 2009, 13:49
use clear(). If you need to remove individual item use removeAt()
clear() will delete all the item.

^NyAw^
12th March 2009, 15:00
Hi,

If you have pointers stored in the list you have to delete them manually:



int iNum = qList.count();
for (int i=0; i<iNumElements; i++)
delete (qList.takeAt(0)); //Always delete element 0

spirit
12th March 2009, 15:03
Hi,

If you have pointers stored in the list you have to delete them manually:



int iNum = qList.count();
for (int i=0; i<iNumElements; i++)
delete (qList.takeAt(0)); //Always delete element 0


of course if you use pointer as container type, but in this case I would use qDeleteAll insted of this loop. ;)

wysota
12th March 2009, 20:27
clear() will delete all the item.

No, it won't. It will only destroy pointers, not the objects behind them (same with removeAt() of course). Delete needs to be called explicitely on each item or, as already said, qDeleteAll() can be used.

Of course all that assumed one "deletes" objects behind pointers. If you have a list of objects then delete is not required as the destructor will be ran when using clear() or removeAt() (but I wouldn't call that "deleting" thus the whole post of mine).