How to drop a custom QStandardItem into a QListView
Solution 1:
It looks like you want setItemPrototype. This provides an item factory for the model, so that it will implicitly use your custom class whenever necessary.
All you need to do is reimplement clone()
in your item class:
class MyItem(QtGui.QStandardItem):
'''This is the item I'd like to drop into the view'''
def __init__(self, parent=None):
super(MyItem, self).__init__(parent)
self.testAttr = 'test attribute value'
def clone(self):
return MyItem()
An then set an instance of that class as the prototype on the receiving model:
# models
model1 = QtGui.QStandardItemModel()
model2 = QtGui.QStandardItemModel()
model2.setItemPrototype(MyItem())
You can forget about all the datastream stuff.
PS:
I suppose I should point out that Qt obviously knows nothing about any python data attributes that may have been set during the item's lifetime, and so those won't get serialized when the item is transferred during a drag and drop operation. If you want to persist data like that, use setData()
with a custom role:
class MyItem(QtGui.QStandardItem):
_TestAttrRole = QtCore.Qt.UserRole + 2
def clone(self):
item = MyItem()
item.testArr = 'test attribute value'
return item
@property
def testAttr(self):
return self.data(self._TestAttrRole)
@testAttr.setter
def testAttr(self, value):
self.setData(value, self._TestAttrRole)