[PyQt] QTreeWidget - drag&drop help

Baz Walter bazwal at ftml.net
Wed Oct 21 19:03:07 BST 2009


Taylor Carrasco wrote:
>> Just trying to do some simple drag drop calls with a QTreeWidget and
>> getting nowheres....
>>
>> What am I missing in dropEvent to get it to actually copy the tree data
>> into the new object?
>> Currently the dragged object disappears after being dragged onto a new top
>> level item, but doesn't show up in the tree.

http://doc.trolltech.com/4.5/model-view-dnd.html


import sys
from PyQt4 import QtGui, QtCore


class TreeWidget(QtGui.QTreeWidget):
     def __init__(self, parent=None):
         QtGui.QTreeWidget.__init__(self, parent)
         self.header().setHidden(True)
         self.setDragDropMode(self.InternalMove)
         self.setDragEnabled(True)
         self.setDropIndicatorShown(True)
         NO_DROP = ~QtCore.Qt.ItemIsDropEnabled
         root = self.invisibleRootItem()
         root.setFlags(root.flags() & NO_DROP)
         for i in xrange(1, 4):
             text = QtCore.QStringList('Parent(%i)' % i)
             parent = QtGui.QTreeWidgetItem(self, text)
             for j in xrange(1, 4):
                 text = QtCore.QStringList('Child(%i-%i)' % (i, j))
                 child = QtGui.QTreeWidgetItem(parent, text)
                 child.setFlags(child.flags() & NO_DROP)
             parent.setExpanded(True)


if __name__ == "__main__":
     app = QtGui.QApplication(sys.argv)
     tree = TreeWidget()
     tree.resize(200, 300)
     tree.move(300, 300)
     tree.show()
     sys.exit(app.exec_())


HTH


More information about the PyQt mailing list