How to send filename along with a drag object ?
I have custom model class having a qlist of a custom metatype.
The custom metatype has a qpixmap & qstring as the member variables. Instances of this metatype are added to model data. The view which uses this model has dragEnabled(true) on its items. Hence when the items from the view are dragged, I want to send the string ( which is stored as part of the metatype ) also. How should I pass this string in the QMimeData that is passed along with the drag object ? Or is there another way to do this ?
The model's mimeData() method is -
Code:
QMimeData *pixmapmodel
::mimeData(const QModelIndexList
&indexes
) const {
if (index.isValid()) {
QPixmap pixmap
= qVariantValue<QPixmap>
(data
(index, Qt
::UserRole));
stream << pixmap;
}
}
mimeData->setData("image/bmp", encodedData);
return mimeData;
}
Re: How to send filename along with a drag object ?
Hi,
You can use a XML document to send data in a structured way for example:
Code:
QDir directory
("/usr/local/home/myhome");
doc.appendChild(root);
QDomElement dirnode
= doc.
createElement("Directory Name");
root.appendChild(dirnode);
value = directory.dirName();
t = doc.createTextNode(value);
dirnode.appendChild(t);
itemData.append(doc.toString());
mimeData->setData("object/x-myApplication-object", itemData);
drag->setMimeData(mimeData);
Then you can read it by:
Code:
if (event->mimeData()->hasFormat("object/x-myApplication-object"))
{
QByteArray objectData
= event
->mimeData
()->data
("object/x-myApplication-object");
doc.setContent(objectData,true);
element = node.toElement();
directory = element.text();
}
As you can see the code uses QByteArray. In this array you can put any kind of data (as long as you know how to read it) so you can pass text, images, etc. So for example you can pass an structure like [lengthOfXml][xmlData][lengthOfImage][imageData]
Re: How to send filename along with a drag object ?
Oh , ok will need to try this out. Thanks