PDA

View Full Version : How to send filename along with a drag object ?



ada10
4th October 2010, 10:50
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 -


QMimeData *pixmapmodel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;

QDataStream stream(&encodedData, QIODevice::WriteOnly);

foreach (QModelIndex index, indexes) {
if (index.isValid()) {
QPixmap pixmap = qVariantValue<QPixmap>(data(index, Qt::UserRole));
stream << pixmap;
}
}

mimeData->setData("image/bmp", encodedData);
return mimeData;
}

qlands
4th October 2010, 13:26
Hi,

You can use a XML document to send data in a structured way for example:



QByteArray itemData;

QDir directory("/usr/local/home/myhome");

QDomDocument doc("myDocument");
QDomElement root = doc.createElement("myDocument");
doc.appendChild(root);

QDomElement dirnode = doc.createElement("Directory Name");
root.appendChild(dirnode);
QString value;
value = directory.dirName();
QDomText t;
t = doc.createTextNode(value);
dirnode.appendChild(t);

itemData.append(doc.toString());

QMimeData *mimeData = new QMimeData;
mimeData->setData("object/x-myApplication-object", itemData);

QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);



Then you can read it by:




if (event->mimeData()->hasFormat("object/x-myApplication-object"))
{
QByteArray objectData = event->mimeData()->data("object/x-myApplication-object");
QDomDocument doc("myDocument");
doc.setContent(objectData,true);

QDomElement docElem = doc.documentElement();
QDomElement element;
QDomNode node = docElem.firstChild();
element = node.toElement();
QString directory;
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]

ada10
5th October 2010, 08:07
Oh , ok will need to try this out. Thanks