Reputation:
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
Upvotes: 0
Views: 3414
Reputation: 20492
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
Upvotes: 2