PDA

View Full Version : deleting QStringList



timmu
17th December 2009, 12:16
How do you completely delete (and deallocate memory) a QStringList?
Is emptying a QStringList the same as deleting it?

Thanks.

aamer4yu
17th December 2009, 12:22
From the docs -

QStringList inherits from QList<QString>.
You dont have pointers stored in QStringList,,only objects. So When you clear it, the object must be deleted or whatever. You should not be concerned about it.
Unless you used something like - QList<QString*>.

timmu
17th December 2009, 12:28
Does this mean that the following will delete a QStringList:



QStringList myList;
........
........
myList = QStringList();


Can the same logic be used to delete a QTableWidget?

caduel
17th December 2009, 14:38
Can the same logic be used to delete a QTableWidget?
No.

You only "delete" heap allocated objects (i.e. stuff allocated with new).
Use delete (or deleteLater()).

Stack-allocated stuff like your QStringList is "destroyed" when it goes out of scope.

{
QStringList xyz;
...
} // xyz is destroyed here automatically


Heap:

QStringList *heap_sl = new QStringList;
...
delete heap_sl;
Heap-allocation is slower and more error-prone. So don't use it when stack based allocation is good enough for your needs.

darshan.hardas
18th December 2009, 12:36
The deletion can be done by using

qDeleteAll(...)

This will call the delete function on each element inside the specified container.

caduel
18th December 2009, 14:36
but please note the difference between

QList<QString*>
and

QList<QString>*
qDeleteAll only makes sense for the former; but also note that a QStringList is probably the better choice anyway.