Preserve QStandardItem subclasses in drag and drop

天涯浪子 提交于 2019-11-28 13:06:16
ekhumoro

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!