PDA

View Full Version : (PyQt) moving items within a QTreeWidget



calireno
12th November 2008, 03:33
Hi,
I've been trying to drap and drop one or more QTreeWidgetItem with PyQt but i can't get it to work properly.

After my QTreeViewWidget is populated i can drap and drop items within the tree with no problems.
But as soon as i add the "dropEvent" function to my code (to get the source item(s) info and the destination item info and run some post actions) the tree doesn't behave the way it did (which was perfect) before i added this function.

What happens "onDrop" is, the item i'm dragging simply disappear from the tree (deleted?) but doesn't "get moved" to its destination node (item i'm dropping the source item on)

You'll find a screenshot of my treeViewWidget so hopefully it will make more sense

Notes :
I'm NOT setting up the QTreeView to a "QDirModel" i am just using a folder icon for some of my items. Again this tree is not a physical directory on a drive i'm pulling the info from a database and rebuilding it as a treeview)

All the informations i'm trying to get out of my dropEvent is my source QTreeWidgetItem(s) and my destination QTreeWidgetItem. That's all.

I've been reading so many pages about drag and drop for the past few days, but i can't figure it out. I started to learn PyQt recently and i usually find the answers i need on internet but this one is killing me. :crying:

Thanks in advance for your help ! :)

This treeViewWidget behaves like i want it to behave :

def __init__(self, parent=None):
super(treeViewWidget, self).__init__(parent)
self.setAcceptDrops(True)
self.setDragEnabled(True)
self.setDragDropMode(QAbstractItemView.InternalMov e)
self.setSelectionMode(QAbstractItemView.ExtendedSe lection)

This one doesn't :


class treeViewWidget(QTreeWidget):
def __init__(self, parent=None):
super(treeViewWidget, self).__init__(parent)
self.setAcceptDrops(True)
self.setDragEnabled(True)
self.setDragDropMode(QAbstractItemView.InternalMov e)
self.setSelectionMode(QAbstractItemView.ExtendedSe lection)

def dropEvent(self, event):
print 'drop'
event.accept()