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 {
}
QMimeData* MyModel::mimeData(const QModelIndexList &indexes) const
{
return new QMimeData(some stuff here);
}
To copy to clipboard, switch view to plain text mode
then it is not clear, who should free the allocated memory.
The original implementation of QAbstractItemModel also allocates new memory without dealocating it :
{
if (indexes.count() <= 0)
return 0;
if (types.isEmpty())
return 0;
encodeData(indexes, stream);
data->setData(format, encoded);
return data;
}
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;
}
To copy to clipboard, switch view to plain text mode
Does anybody already encountered a problem or have any suggestions?
Thanx in advance!
Bookmarks