Yevgen
6th March 2014, 11:01
Hello Qt Community,
I'm wondering, what is the best way to implement the mimeData() method in my custom model class (inheriting QAbstractItemModel) without causing a memory leak.
If I write somethink like this
QMimeData* MyModel::mimeData(const QModelIndexList &indexes) const
{
return new QMimeData(some stuff here);
}
then it is not clear, who should free the allocated memory.
The original implementation of QAbstractItemModel also allocates new memory without dealocating it :
QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
{
if (indexes.count() <= 0)
return 0;
QStringList types = mimeTypes();
if (types.isEmpty())
return 0;
QMimeData *data = new QMimeData();
QString format = types.at(0);
QByteArray encoded;
QDataStream stream(&encoded, QIODevice::WriteOnly);
encodeData(indexes, stream);
data->setData(format, encoded);
return data;
}
Does anybody already encountered a problem or have any suggestions?
Thanx in advance!
I'm wondering, what is the best way to implement the mimeData() method in my custom model class (inheriting QAbstractItemModel) without causing a memory leak.
If I write somethink like this
QMimeData* MyModel::mimeData(const QModelIndexList &indexes) const
{
return new QMimeData(some stuff here);
}
then it is not clear, who should free the allocated memory.
The original implementation of QAbstractItemModel also allocates new memory without dealocating it :
QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
{
if (indexes.count() <= 0)
return 0;
QStringList types = mimeTypes();
if (types.isEmpty())
return 0;
QMimeData *data = new QMimeData();
QString format = types.at(0);
QByteArray encoded;
QDataStream stream(&encoded, QIODevice::WriteOnly);
encodeData(indexes, stream);
data->setData(format, encoded);
return data;
}
Does anybody already encountered a problem or have any suggestions?
Thanx in advance!