Results 1 to 4 of 4

Thread: Drag and drop in QTreeView

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drag and drop in QTreeView

    I have a subclassed QTreeView with a custom model. The model interfaces with a QList. I want to implement a custom internal drag and drop for the view. I already have drag and drop working for dragging files into the view from external file viewers. Basically what I want to do is that when rows are dragged in the view itself, the QList order changes: ie. I takeAt() and insert() in the list.
    The docs are a little unclear as to what needs to be overridden in the model and the view. Currently, I have subclassed QMimeData and added an API to get which items is the list are being moved. I think that it won't be a problem to use that info to insert them back into the list, however I am not entirely sure as to what I should do with the rows in the view/model. The docs mention that you should implement removeRows() for the model, but they don't go on to say if you should call that function in the model explicitly yourself, or call it via the view, or what. Does anyone have any experience with this?

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

    Default Re: Drag and drop in QTreeView

    You have to reimplement QAbstractItemModel::dropMimeData().
    The drag/drop stuff can be handled entirely in the model.
    (Don't forget to implement mimeTypes() and flags() to support drops!)
    Just insert (and fill) the appropriate rows in dropMimeData().
    Make sure that the appropriate signals are emitted.
    )

    HTH
    PS: The docs of dropMimeData() link to an example of DnD and model/views. Read it.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drag and drop in QTreeView

    Usually it's not even necessary to reimplement mimeData() nor dropMimeData() because it's already implemented fairly well in QAbstractItemModel. See this thread for more details and for an example: http://www.qtcentre.org/forum/f-newb...-10454.html#14
    J-P Nurmi

  4. #4
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and drop in QTreeView

    Thanks for the info, I'll take a look. As I said, the docs are a bit vague on this, and didn't give the answer that you outlined in that thread. Maybe I am just blind, but I couldn't find a clear answer on how to do it.

Similar Threads

  1. Drag and Drop on Whitespace of QTreeView
    By T1c4L in forum Qt Programming
    Replies: 6
    Last Post: 14th May 2008, 13:29
  2. Drag & drop items on the same QTreeView
    By wind in forum Qt Programming
    Replies: 2
    Last Post: 11th October 2006, 14:29
  3. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41
  4. drag and drop from QTreeView
    By Untersander in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2006, 09:00
  5. Drag & drop for QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2006, 14:32

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.