[PyQt] Moving items within a QTreeWidget

calireno talonrenaud at hotmail.com
Thu Nov 13 23:18:52 GMT 2008


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.

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.InternalMove)
          self.setSelectionMode(QAbstractItemView.ExtendedSelection)



This one doesn't :

Qt Code:

      class treeViewWidget(QTreeWidget):
          def __init__(self, parent=None):
              super(treeViewWidget, self).__init__(parent)
              self.setAcceptDrops(True)
              self.setDragEnabled(True)
              self.setDragDropMode(QAbstractItemView.InternalMove)
              self.setSelectionMode(QAbstractItemView.ExtendedSelection)
 
          def dropEvent(self, event):
              print 'drop'
              event.setDropAction(QtCore.Qt.MoveAction)
              event.accept() 



http://www.nabble.com/file/p20491149/tree.png tree.png 
-- 
View this message in context: http://www.nabble.com/Moving-items-within-a-QTreeWidget-tp20491149p20491149.html
Sent from the PyQt mailing list archive at Nabble.com.



More information about the PyQt mailing list