PDA

View Full Version : Changing row order by dragging them



shadowfax
4th May 2011, 13:50
I want to change row order in my custom model by dragging them. I've table view and custom model using QAbstractTableModel. I need the same behaviour as in listWidget , when I can reorder items by dragging them. What I need to do to achieve the same effect in model/view ?

wysota
4th May 2011, 19:29
You need to teach your model to do that by implementing QAbstractItemModel::mimeData() and QAbstractItemModel::dropMimeData().

shadowfax
30th May 2011, 15:19
I'm having problem with mimeData and dropMimeData implementation. If I implement both of them only mimeData method is working. When I implement only one of them they're correctly invoked. What should I do to have both methods working correctly ?

wysota
30th May 2011, 15:20
How did you reimplement them?

shadowfax
30th May 2011, 15:50
I reimplemented them in the most basic form in order to check if they're working:



def dropMimeData(self, data, action, row, column, parentIndex):
print 'drop'
return True

def mimeData(self, indexes):
print 'drag'
mime = QtCore.QMimeData()
return mime

wysota
30th May 2011, 16:05
Essentially your mimeData implementations says "I do not accept any drops" so dropMimeData will never be called.

shadowfax
1st June 2011, 15:18
How should I enable drop in mimeData method ? According to Qt reference mimeData contains data corresponding to indexes, so what I need to do to enable drop in this method ? I was thinking that mimeData method only returns mimeData object based on indexes. If there is any example that shows how to do this ?

wysota
1st June 2011, 15:21
Did you reimplement mimeTypes() as well? I confused the two methods a bit. Your mimeData() actually says "I don't have anything interesting to drag" and not "I don't accept drops". That's what mimeTypes() returns. Read the docs for all three methods, you'll need them all.