PDA

View Full Version : Drag and Drop MimeData



Zephro
19th February 2006, 01:45
Hi all!

Ok so I'm trying to get drag and drop operations to transfer my own custom data type which is a tree.

So is it a good idea to reimplement QDrag and the DropEvents to carry a different datastructure? Looks fiddly.

Or am I going to have to work out how to turn the data structure into a bytestream? I'm fairly sure this is the right route, but not quite sure where to start. Any suggestions on how to do this?

It's a normal enough binary tree with left/right Node pointers.

wysota
20th February 2006, 13:50
It depends what techniques you use in your program (for example the MVC pattern has its own support for D&D).

In most basic approach you need to use QMimeData to carry the data for you and dropEvent to handle the drop itself.

Zephro
20th February 2006, 19:02
Ah I fixed this, in a messy way but still fixed it.

I just cast the pointer into an int and stored that as mimedata. Then cast it back on the other side. Worked as the objects are on the heap. Still used messy casting.

wysota
20th February 2006, 19:20
That's not very elegant ;)

You may try:


QByteArray myData;
myData = myStructObj.serialize(); // *
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-mystruct", myData);
*) - you have to provide this operator yourself, for example using a QDataStream or something like that

For simple structures using a TSV or CSV is a nice way to go too. If you don't plan to drag the item out of your application, you can stick with pointers too. Just dragging your "int" to some text editor or something like that may provide weird results ;)

Zephro
20th February 2006, 19:24
Ah it's just got to stay in the same application, so working out how to serialize the entire Tree structure seemed a bit overkill when it was on the heap. Reuse just isn't an issue.

EDIT: It's for a uni project, so getting it working is the bigger issue at the moment ¬_¬

kiker99
15th May 2006, 20:42
*) - you have to provide this operator yourself, for example using a QDataStream or something like that


can you give a small hint how to do that? QDataStream doesn't have that big selection of functions to get your Objects in there. Can you store any data from a pointer in a DataStream?

wysota
16th May 2006, 00:58
Not from a pointer. Reimplement the operator for the object itself and for the pointer.

kiker99
16th May 2006, 13:26
Not from a pointer. Reimplement the operator for the object itself and for the pointer.

yeah, but how do i have to implement that operator? Do I need to build something similar to a string out of the objects attributes? And how do I get the real Object back at the point where the user ended the drag'n'drop operation?

wysota
16th May 2006, 13:38
yeah, but how do i have to implement that operator? Do I need to build something similar to a string out of the objects attributes? And how do I get the real Object back at the point where the user ended the drag'n'drop operation?

It doesn't matter how you implement it. It only matters that it has to be converted to a stream of bytes and then the conversion has to be reversed (by implementing deserialisation from the data stream).

For example you could store an xml representation of your object in the stream (although it would be an overkill).

kiker99
16th May 2006, 16:28
ok, thanks a lot :)

gfunk
16th May 2006, 19:20
so you passed a string containing the text representation of the int value of the pointer into setData? hilarious :p