Results 1 to 5 of 5

Thread: Drag and Drop

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Drag and Drop

    Hi,
    i try to implement DragAndDrop Suuport in my simple calender application. It is model/view based, so I read the trolltechs docu:
    http://doc.trolltech.com/4.3/model-view-dnd.html

    It is not clear for me how I have to proceed.
    My application is a table view which works like the outlook calender. You have events in the calender, these events have a start date, end date, event number and two booleans.
    So i like to drag and drop these events to a different date in my table.
    Do you know, what I have to implement to reach this ?

    What do Ihave to do with the method:
    QStringList DragDropListModel::mimeTypes() const

    and what do I have to do with the method:
    QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes) const

    ?

    Thanks,
    Alex

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

    Default Re: Drag and Drop

    mimeTypes() should return a list of mime-types your model can generate and handle. You'll be using a custom type, so it should probably be something like application/x-calendar-event (you can put whatever you want after the "x-" part, just stick to the same name everywhere).

    mimeData() should encode (serialize) the object you want to drag (given by the list of model indexes) into a byte array which can then be transferred to another object and decoded using dropMimeData() or another drop mechanism (dropEvent() in case of Qt apps).

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

    starcontrol (30th April 2008)

  4. #3
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Drag and Drop

    What would be the best way to extract QModelIndexList from QMimeData inside dropMimeData()? I call data->data("application/x-qabstractitemmodeldatalist") to get an instance of QByteArray, but am not sure how to proceed from this point forward

    I tried the following:

    Qt Code:
    1. QByteArray buf = data->data("application/x-qabstractitemmodeldatalist");
    2. QDataStream s(&buf, QIODevice::ReadOnly);
    3. QModelIndexList list;
    4. s >> list;
    To copy to clipboard, switch view to plain text mode 

    but the compilation fails because there is no operator>>(QDataStream& s, QModelIndex& idx). I could implement the operator but I do not know how QModelIndex is encoded in QByteArray.
    Last edited by yuriry; 26th September 2008 at 19:48.

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and Drop

    A list of QModelIndexes makes only sense within an application (at least, I can't think of another scenario right now).
    Within an application you can access the QMimeData object you created at the drop site.
    So you can just create a subclass of QMimeData in mimeData(). This subclass could feature a QList<QModelIndex> as a member variable.
    Use qobject_cast to case to that subclass and just get the QList<QModelIndex> without worrying about how to store those as binary mime data.

    HTH

  6. The following user says thank you to caduel for this useful post:

    yuriry (26th September 2008)

  7. #5
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop

    Thanks, Caduel. I also noticed that I missed the point of mimeData(): it does not serialize model indexes but, instead, the items of data corresponding to those indexes.

Similar Threads

  1. Drag and Drop
    By MrShahi in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2008, 15:01
  2. Problem with a cursor during Drag and Drop
    By mtrpoland in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2007, 15:46
  3. Replies: 7
    Last Post: 8th September 2006, 16:19
  4. Drag and drop outside the application
    By jpn in forum Newbie
    Replies: 7
    Last Post: 27th August 2006, 15:37
  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
  •  
Qt is a trademark of The Qt Company.