Python 从PyQt5迁移到PySide2时的QVariant替代方案

Python 从PyQt5迁移到PySide2时的QVariant替代方案,python,pyqt5,pyside2,Python,Pyqt5,Pyside2,由于以下代码,我在从PyQt5切换到PySide2时遇到一些问题: class EnumModel(QtCore.QAbstractListModel): def __init__(self, list_of_enums): """ Enumeration model :param list_of_enums: list of enumeration values to show """ QtCore.QA

由于以下代码,我在从PyQt5切换到PySide2时遇到一些问题:

class EnumModel(QtCore.QAbstractListModel):

    def __init__(self, list_of_enums):
        """
        Enumeration model
        :param list_of_enums: list of enumeration values to show
        """
        QtCore.QAbstractListModel.__init__(self)
        self.items = list_of_enums

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.items)

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if index.isValid() is True:
            if role == QtCore.Qt.DisplayRole:
                return QtCore.QVariant(self.items[index.row()].value[0])
            elif role == QtCore.Qt.ItemDataRole:
                return QtCore.QVariant(self.items[index.row()].value[0])
        return QtCore.QVariant()
代码在PyQt5下运行良好

在迁移过程中,我发现:

PySide仅支持PyQt的API 2(参见PSEP 101)了解详细信息。 因此Qt类,如QStrings、QStringLists和QVariants是 PySide上不可用。相反,您应该只使用本机Python 数据类型

因此,解决方案是(我想)通过
str
简单地更改
QVariant
。当我这样做时,类不会抛出任何错误,但它也不会显示模型

实际上,函数
data
正在接收
role=13
,而不是
role=QtCore.Qt.DisplayRole

我不知道这是PySide2的一个bug(在Linux下有点bug),还是因为其他原因

一个最起码可行的例子是:

from PySide2.QtWidgets import *
from PySide2 import QtCore
from enum import Enum


class SomeEnum(Enum):
    A = 'A'
    B = 'B'
    C = 'C'


class EnumModel(QtCore.QAbstractListModel):

    def __init__(self, list_of_enums):
        """
        Enumeration model
        :param list_of_enums: list of enumeration values to show
        """
        QtCore.QAbstractListModel.__init__(self)
        self.items = list_of_enums

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.items)

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if index.isValid() is True:
            if role == QtCore.Qt.DisplayRole:
                return self.items[index.row()].value[0]
            elif role == QtCore.Qt.ItemDataRole:
                return self.items[index.row()].value[0]
            else:
                print('not recognised')
        return ""


if __name__ == '__main__':
    import sys

    model = EnumModel([SomeEnum.A, SomeEnum.A, SomeEnum.B, SomeEnum.C])

    app = QApplication(sys.argv)
    lst = QListView()
    lst.setModel(model)
    lst.show()
    sys.exit(app.exec_())

问题在于,当视图需要与角色相关的信息时,
Qt::SizeHintRole
(13)将传递一个空字符串,而必须返回None或干脆不返回任何内容,因为它会干扰其他角色:

def数据(self,index,role=QtCore.Qt.DisplayRole):
如果index.isValid():
打印(角色)
如果角色==QtCore.Qt.DisplayRole:
返回self.items[index.row()]值[0]
elif角色==QtCore.Qt.ItemDataRole:
返回self.items[index.row()]值[0]
其他:
打印('未识别')

枚举列表的值是多少,请提供一个。我通过将
QtCore.QVariant(self.items[index.row()].value[0])
更改为
self.items[index.row()]
,传递
list\u of_enums=[“a”、“b”、“c”]
来测试代码,它工作正常,因此我认为错误在
list\u of__enums
。如果您不打算显式返回值,您应该调用基类
data
方法。
role=13
SizeIntrole
,那么您将为此返回什么值呢?您好,所以我做了一个完整的示例。在Ubuntu18.04、PySide2 5.12.3下,列表中没有显示任何内容。