How do you completely delete (and deallocate memory) a QStringList?
Is emptying a QStringList the same as deleting it?
Thanks.
Printable View
How do you completely delete (and deallocate memory) a QStringList?
Is emptying a QStringList the same as deleting it?
Thanks.
From the docs -
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.Quote:
QStringList inherits from QList<QString>.
Unless you used something like - QList<QString*>.
Does this mean that the following will delete a QStringList:
Can the same logic be used to delete a QTableWidget?
No.Quote:
Can the same logic be used to delete a QTableWidget?
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.
Heap:
Heap-allocation is slower and more error-prone. So don't use it when stack based allocation is good enough for your needs.
The deletion can be done by using
qDeleteAll(...)
This will call the delete function on each element inside the specified container.
but please note the difference between
andCode:
QList<QString*>
qDeleteAll only makes sense for the former; but also note that a QStringList is probably the better choice anyway.Code:
QList<QString>*