[PyKDE] Dragdrop with a QTreeView?
Adam Tenderholt
atenderholt at gmail.com
Fri Jun 16 23:45:30 BST 2006
I came up with a general method that mostly works with Qt 4.1.3.
>From what I can tell, the dataChanged() signal is supposed to be
emitted at the end of the subclass' dropMimeData implementation after
the new information is added to the target model. If it is connected
to custom slot, say slotDeleteMoved, then the source model can be
notified when the drop is successful. Some pseudo-code (apologies for
mixing C- and python-like syntaxes):
sourceModel.mimeData(self,indexes){
self.movedItems=[]
#code to build mimedata
for index in indexes:
.....
self.movedItems.append(IndexOfItemBeingMoved)
return MimeData
}
sourceModel.slotDeleteMoved(self){
#make sure movedItems exists and has elements
beginRemoveRows(...)
#remove items in movedItems from model
endRemoveRows()
self.reset() #updates widgets more effectively
}
targetModel.dropMimeData(self,...){
#handle dropped data, calling beginInsertRows(), etc. as necessary
emit dataChanged() signal
}
And finally, make sure dataChanged() and slotDeleteMoved() are
connected. I occasionally get KeyErrors from self.obj_dict if I drag
and drop more than actually needed, but the text in the widget appears
as expected although I haven't yet checked the data integrity in the
underlying model. Also, I forgot to mention that this works for the
cases where source and target are the same model or different models.
Adam
More information about the PyQt
mailing list