0

I am trying to handle a drop event on a TreeWidget from itself by overriding the dropEvent method. Ultimately, I need to get a handle to the TreeWidgetItem being dropped. So far, the only useful information I get from the event regarding the dropped item is a QByteArray that seems to contain text from the item being dropped, except that it's poorly formatted with lots of spaces and a bunch of non-printable characters.

Any help would be greatly appreciated.

edit:

Here is the code, as asked, but I'm really not doing anything special, I'm literally just reading the only type of data contained in the mimeData of the drop event. It sounds as though I'm going to have to override the Drag event?? And add some type of identifier to allow me to get a handle back to the original QTreeWidget??

def dropEvent( self, event ):
    data = event.mimeData().data( 'application/x-qabstractitemmodeldatalist' )
    print data
  • It would be helpful if you provided some sample code, as far as i understand you should encode the data you need in a QMimeData object and not expect to get a handle on the QTreeWidgetItem – armonge Jan 24 '11 at 21:25

1 Answers1

2

not quite sure I'm understanding the question correctly, but your mime data comes from the startDrag method, where you've created a QMimeData object, set it's type and supplied data accordingly. In your dropEvent method check the type of the incoming data and process it accordingly or ignore if you don't recognize the type.

Also take a look at the documentation here: Drag and Drop it should give you an idea on how drag and drop works in qt

I also made a small example here, see if it would work for you:

import sys
from PyQt4 import QtGui, QtCore

class TestTreeWidget(QtGui.QTreeWidget):
    def __init__(self, parent = None):
        super(TestTreeWidget, self).__init__(parent)
        self.setDragEnabled(True)
        self.setAcceptDrops(True)

    def startDrag(self, dropAction):
        # create mime data object
        mime = QtCore.QMimeData()
        mime.setData('application/x-item', '???')
        # start drag 
        drag = QtGui.QDrag(self)
        drag.setMimeData(mime)        
        drag.start(QtCore.Qt.CopyAction | QtCore.Qt.CopyAction)

    def dragMoveEvent(self, event):
        if event.mimeData().hasFormat("application/x-item"):
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
        else:
            event.ignore()

    def dragEnterEvent(self, event):
        if (event.mimeData().hasFormat('application/x-item')):
            event.accept()
        else:
            event.ignore()    

    def dropEvent(self, event): 
        if (event.mimeData().hasFormat('application/x-item')):
            event.acceptProposedAction()
            data = QtCore.QString(event.mimeData().data("application/x-item"))
            item = QtGui.QTreeWidgetItem(self)
            item.setText(0, data)
            self.addTopLevelItem(item)
        else:
            event.ignore()    

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.view = TestTreeWidget(self)
        self.view.setColumnCount(1)
        item0 = QtGui.QTreeWidgetItem(self.view)
        item0.setText(0, 'item0')
        item1 = QtGui.QTreeWidgetItem(self.view)
        item1.setText(0, 'item1')
        self.view.addTopLevelItems([item0, item1])

        self.setCentralWidget(self.view)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

also you may want to take a look at the similar post here: QTreeView with drag and drop support in PyQt

hope this helps, regards

Community
  • 1
  • 1
serge_gubenko
  • 20,186
  • 2
  • 61
  • 64
  • 1
    Thanks. Right now, I'm not overriding the startDrag method. I was hoping the default implementation of startDrag for QTreeWidget placed something useful in the mimedata to get a handle to the original QTreeWidgetItem. Based on your example, it looks like I'll need to do it myself. –  Jan 26 '11 at 02:16
  • Thank you! I've been mucking around with drag/drop in Qt for hours now and this finally debugged me. – Spencer Jul 22 '18 at 02:34