<HTML><HEAD></HEAD>
<BODY dir=ltr>
<DIV dir=ltr>
<DIV style="FONT-FAMILY: 'Calibri'; COLOR: #000000; FONT-SIZE: 12pt">
<DIV>Hi everyone,</DIV>
<DIV> </DIV>
<DIV>I'm encountering some poblems trying to implement drag and drop for a
custom QListWidgetItem. Here is some example code:</DIV>
<DIV> </DIV>
<DIV>from PyQt4 import QtGui, QtCore</DIV>
<DIV>import sys, os</DIV>
<DIV> </DIV>
<DIV>class
MyListWidgetItem(QtGui.QListWidgetItem): </DIV>
<DIV> def __init__(self, label, data, parent=None):</DIV>
<DIV> super(QtGui.QListWidgetItem,
self).__init__(label, parent=parent)</DIV>
<DIV> self.data = data</DIV>
<DIV> </DIV>
<DIV> def GetData(self):</DIV>
<DIV> return self.data</DIV>
<DIV> </DIV>
<DIV>class MyListWidget(QtGui.QListWidget):</DIV>
<DIV> def __init__(self, type, parent=None):</DIV>
<DIV> super(MyListWidget,
self).__init__(parent)</DIV>
<DIV>
self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)</DIV>
<DIV>
self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)</DIV>
<DIV> self.setAcceptDrops(True)</DIV>
<DIV>
self.viewport().setAcceptDrops(True)</DIV>
<DIV>
self.setDropIndicatorShown(True)</DIV>
<DIV> </DIV>
<DIV> def startDrag(self, supportedActions):</DIV>
<DIV> drag = QtGui.QDrag(self)</DIV>
<DIV> t = [i.GetData() for i in
self.selectedItems()]</DIV>
<DIV> mimeData =
self.model().mimeData(self.selectedIndexes())</DIV>
<DIV> mimeData.setText(str(t))</DIV>
<DIV> drag.setMimeData(mimeData)</DIV>
<DIV> if
drag.start(QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:</DIV>
<DIV> for item
in self.selectedItems():</DIV>
<DIV>
self.takeItem(self.row(item))</DIV>
<DIV>
</DIV>
<DIV> def dragEnterEvent(self, event):</DIV>
<DIV> if
event.mimeData().hasUrls():</DIV>
<DIV>
event.ignore()</DIV>
<DIV> else:</DIV>
<DIV>
event.accept()</DIV>
<DIV> </DIV>
<DIV> def dragMoveEvent(self, event):</DIV>
<DIV> if
event.mimeData().hasUrls():</DIV>
<DIV>
event.ignore()</DIV>
<DIV> else:</DIV>
<DIV>
event.accept()</DIV>
<DIV> </DIV>
<DIV> def dropEvent(self, event):</DIV>
<DIV> if
event.mimeData().hasUrls():</DIV>
<DIV>
event.ignore()</DIV>
<DIV> if isinstance(event.source(),
MyListWidget):</DIV>
<DIV>
event.setDropAction(QtCore.Qt.MoveAction)</DIV>
<DIV>
super(MyListWidget, self).dropEvent(event)</DIV>
<DIV> else:</DIV>
<DIV>
event.ignore()</DIV>
<DIV> </DIV>
<DIV> def dropMimeData(self, index, mimedata, action):</DIV>
<DIV> super(MyListWidget,
self).dropMimeData(index, mimedata, action)</DIV>
<DIV> return True</DIV>
<DIV> </DIV>
<DIV>class Test(QtGui.QMainWindow):</DIV>
<DIV> def __init__(self):</DIV>
<DIV>
super(QtGui.QMainWindow,self).__init__()</DIV>
<DIV> myQWidget =
QtGui.QWidget()</DIV>
<DIV> myBoxLayout =
QtGui.QVBoxLayout()</DIV>
<DIV>
myQWidget.setLayout(myBoxLayout)</DIV>
<DIV>
self.setCentralWidget(myQWidget)</DIV>
<DIV> </DIV>
<DIV> self.listWidgetA =
MyListWidget(self)</DIV>
<DIV> self.listWidgetB =
MyListWidget(self)</DIV>
<DIV> </DIV>
<DIV> for i in range(5):</DIV>
<DIV>
listItemAInstance = MyListWidgetItem(str(i), i, parent=self.listWidgetA)</DIV>
<DIV> </DIV>
<DIV>
myBoxLayout.addWidget(self.listWidgetA) </DIV>
<DIV>
myBoxLayout.addWidget(self.listWidgetB) </DIV>
<DIV> </DIV>
<DIV>if __name__ == '__main__':</DIV>
<DIV> app = QtGui.QApplication(sys.argv)</DIV>
<DIV> dialog_1 = Test()</DIV>
<DIV> dialog_1.show()</DIV>
<DIV> dialog_1.resize(480,320)</DIV>
<DIV> sys.exit(app.exec_())</DIV>
<DIV> </DIV>
<DIV>My custom class MyListWidgetItem has a 'data' field which in my real
program holds some information related to the item. If you run the code and try
to drag and drop items from the top list to the bottom one everything works, but
if you then try to bring them back to the top list you get this error:</DIV>
<DIV> </DIV>
<DIV>Traceback (most recent call last):</DIV>
<DIV> File "C:\Users\Massi\Desktop\t.py", line 23, in startDrag</DIV>
<DIV> t = [i.GetData() for i in self.selectedItems()]</DIV>
<DIV>AttributeError: 'QListWidgetItem' object has no attribute 'GetData'</DIV>
<DIV> </DIV>
<DIV>It seems pretty clear that the default drag and drop behaviour ignores that
the list items have been subclassed, so I wonder which is the best to deal with
this situation. Any help is really appreciated.</DIV>
<DIV>Thanks in advance!</DIV></DIV></DIV></BODY></HTML>