If both key and value are stored as pointers. You need to execute qDeleteAll
twice for keys and for values. Order doesn't matter. Simple example:
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QHash>
class MyKey
{
public:
MyKey(int val)
{
m_val = val;
qDebug() << "ClassKey()";
}
~MyKey()
{
qDebug() << "~ClassKey() " << m_val;
}
private:
int m_val;
};
class MyValue
{
public:
MyValue(int val)
{
m_val = val;
qDebug() << "ClassValue()";
}
~MyValue()
{
qDebug() << "~ClassValue() " << m_val;
}
private:
int m_val;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QHash<MyKey *, MyValue *> hash;
for (int i = 0; i < 10; ++i)
{
hash.insert(new MyKey(i), new MyValue(10 + i));
}
qDeleteAll(hash.keyBegin(), hash.keyEnd());
qDeleteAll(hash.begin(), hash.end());
hash.clear();
return a.exec();
}