Results 1 to 11 of 11

Thread: Drag and Drop MimeData

  1. #1
    Join Date
    Feb 2006
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Drag and Drop MimeData

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop MimeData

    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.

  3. #3
    Join Date
    Feb 2006
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop MimeData

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop MimeData

    That's not very elegant

    You may try:

    Qt Code:
    1. QByteArray myData;
    2. myData = myStructObj.serialize(); // *
    3. QMimeData *mimeData = new QMimeData;
    4. mimeData->setData("application/x-mystruct", myData);
    To copy to clipboard, switch view to plain text mode 
    *) - 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

  5. #5
    Join Date
    Feb 2006
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop MimeData

    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 ¬_¬

  6. #6
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and Drop MimeData

    Quote Originally Posted by wysota
    *) - 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?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop MimeData

    Not from a pointer. Reimplement the operator for the object itself and for the pointer.

  8. #8
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and Drop MimeData

    Quote Originally Posted by wysota
    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?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop MimeData

    Quote Originally Posted by kiker99
    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).

  10. The following user says thank you to wysota for this useful post:

    kiker99 (16th May 2006)

  11. #10
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and Drop MimeData

    ok, thanks a lot

  12. #11
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Drag and Drop MimeData

    so you passed a string containing the text representation of the int value of the pointer into setData? hilarious
    Software Engineer



Similar Threads

  1. Replies: 1
    Last Post: 8th January 2009, 17:40
  2. drag and drop QToolButton in QToolBar
    By NBilal in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 20:11
  3. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 16:56
  4. Drag and Drop QTableWidget and QTableView
    By scorpion11 in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 09:33
  5. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.