As explained in this answer, you can use setItemPrototype to provide an item factory for a model. However, as also stated in the answer, only certain kinds of information are transferred during a drag and drop operation. For a QStandardItem
, this means only the item flags and item data. There is no way to preserve the specific subclass of the item if there are multiple subclasses being used. A model can have only one prototype, and that is used for all items that are created internally by Qt.
This means you should not use multiple QStandardItem
subclasses if you need to distinguish between different item types. Instead, you should use a single subclass and reimplement QStandardItem.type to distinguish between them:
class MyItem(QtGui.QStandardItem):
TypeItemA = QtGui.QStandardItem.UserType
TypeItemB = QtGui.QStandardItem.UserType + 1
TypeItemC = QtGui.QStandardItem.UserType + 2
def clone(self):
return MyItem()
def type(self):
return self.data(QtCore.Qt.UserRole + 1000)
def setType(self, value):
self.setData(QtCore.Qt.UserRole + 1000, value)
...
itemA = MyItem(self)
itemA.setType(MyItem.TypeItemA)
itemB = MyItem(self)
itemB.setType(MyItem.TypeItemB)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…