Get drag-n-drop qtreewidget items - Python
Question: Is there a way I can get the items being drag/dropped and their destination
parent?

In an ideal scenario what I want to happen is once the **dropEvent** finishes,
it prints the qtreewidgetitems which were moved, as well as the new parent
which the items were moved to. The parent would either be the qtreewidget
itself or another qtreewidgetitem depending on where the drop happened.

Can someone help me out here please?

Below is the code i have so far.

[![enter image description
here](http://i.stack.imgur.com/7XQRQ.png)](http://i.stack.imgur.com/7XQRQ.png)

    
    
    # Imports
    # ------------------------------------------------------------------------------
    import sys
    from PySide import QtGui, QtCore, QtSvg
    
    class TreeNodeItem( QtGui.QTreeWidgetItem ):
        def __init__( self, parent, name="" ):
            super( TreeNodeItem, self ).__init__( parent )
            self.setText( 0, name )
            self.stuff = "Custom Names - " + str(name)
    
    class TreeWidget(QtGui.QTreeWidget):
        def __init__(self, parent=None):
            QtGui.QTreeWidget.__init__(self, parent)
            self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
            self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
            self.setItemsExpandable(True)
            self.setAnimated(True)
            self.setDragEnabled(True)
            self.setDropIndicatorShown(True)
            self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
            self.setAlternatingRowColors(True)
    
        # def dropEvent(self, event):
        #     print "finished"
        def dropEvent(self, event):
            return_val = super( TreeWidget, self ).dropEvent( event )
            print ("Drop finished")
            d = event.mimeData()
            print d, event.source()
            return return_val
    
    # Main
    # ------------------------------------------------------------------------------
    class ExampleWidget(QtGui.QWidget):
    
        def __init__(self,):
            super(ExampleWidget, self).__init__()
    
            self.initUI()
    
        def initUI(self):
    
            # formatting
            self.resize(250, 400)
            self.setWindowTitle("Example")
    
            # widget - passes treewidget
            self.itemList = QtGui.QTreeWidget()
            self.itemList = TreeWidget()
    
            headers = [ "Items" ]
            self.itemList.setColumnCount( len(headers) )
            self.itemList.setHeaderLabels( headers )            
    
            # layout Grid - row/column/verticalpan/horizontalspan
            self.mainLayout = QtGui.QGridLayout(self)
            self.mainLayout.setContentsMargins(5,5,5,5)
            self.mainLayout.addWidget(self.itemList, 0,0,1,1)
    
            # display
            self.show()
    
        # Functions
        # --------------------------------------------------------------------------   
        def closeEvent(self, event):
            print "closed"
    
    
        def showEvent(self, event):
            print "open"
            for i in xrange(20):
                TreeNodeItem( parent=self.itemList , name=str(i) )
    
    # Main
    # ------------------------------------------------------------------------------
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        ex = ExampleWidget()
        sys.exit(app.exec_())
    


Answer: I would suggest using a View/Model approach. Decoding the
'application/x-qabstractitemmodeldatalist' will only return the item dragged,
and the dropMimeData isn't used in the QTreeWidget.

Look here for an example.
<https://wiki.python.org/moin/PyQt/Handling%20Qt's%20internal%20item%20MIME%20type>

