PDA

View Full Version : Drag and drop in QTreeView



Valheru
27th July 2008, 02:01
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?

caduel
27th July 2008, 08:23
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.

jpn
27th July 2008, 08:35
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-newbie-4/t-whole-row-dragdrop-10454.html#14

Valheru
27th July 2008, 09:36
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.