Results 1 to 5 of 5

Thread: Dragging a pointer

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Dragging a pointer

    Hi!

    I feel the need to drag and drop a pointer to an object (application internally only), but my skills are too weak to accomplish this.

    I figure the way to do it is to construct a QByteArray from a pointer to MyObject and put it into a QMimeData object.

    Drag:
    Qt Code:
    1. MyObject *myobject = new MyObject();
    2.  
    3. QMimeData *mimeData = new QMimeData;
    4. mimeData->setData("myobject/pointer", QByteArray( (char*)&myobject, sizeof(myobject) ));
    To copy to clipboard, switch view to plain text mode 

    This code takes the address of the myobject pointer and converts it to a char pointer. That char pointer is passed as argument to the QByteArray constructor along with the right size, so that the QByteArray should contain the bytes of the address of my object. Right?

    Now the unwrapping in the drop:

    Qt Code:
    1. MyObject* p;
    2. char* data = event->mimeData()->data("myobject/pointer").data();
    3. p = (MyObject*)(*data);
    4. qDebug() << p;
    5. /* BOOM */
    To copy to clipboard, switch view to plain text mode 

    mimeData()->data() returns a QByteArray.
    QByteArray.data() returns a char* to the data.
    So I hoped to take the contents of the data with *data and cast it to a pointer to MyObject, but it crashes.

    Little help please?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Dragging a pointer

    maybe better keep objects in QMap or in a QHash and pass object id through d&d and then get the pointer from QMap by id.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    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: Dragging a pointer

    You are storing a (temporary) pointer to a pointer to an object, not a pointer to an object.

  4. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dragging a pointer

    It works like this:

    Qt Code:
    1. MyObject* p;
    2. memcpy(&p, event->mimeData()->data("myobject/pointer").data(), sizeof(p));
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: Dragging a pointer

    How might you accomplish this in python? I'd rather not serialize my objects (which are not QObjects), I want to add mime data for it so I can drag to the desktop and other applications but when dragging internally I really don't want to deal with the overhead.

    Is it possible to subclass QDrag, and add a MyDragClass.object() or something along those lines? (requires an isinstance(MyDragClass, dragObject) in lots of places though)

Similar Threads

  1. Replies: 4
    Last Post: 16th November 2008, 13:53
  2. Dynamic pointer type
    By estanisgeyer in forum General Programming
    Replies: 3
    Last Post: 9th October 2008, 16:51
  3. pointer in a class
    By mickey in forum General Programming
    Replies: 23
    Last Post: 26th May 2008, 15:52
  4. Delete auto pointer from QMultiMap
    By phillip_Qt in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2007, 17:29
  5. Passing a pointer in Signal/Slot Connection
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2007, 19:04

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.