PyQt5 - 使用 QSortFilterProxyModel 时退出代码 3 --> filterAcceptsRow

PyQt5 - exit code 3 when using QSortFilterProxyModel --> filterAcceptsRow

我正在尝试在 PyQt5 中使用 QSortFilterProxyModel (Python 3.7 -IDE PyCharm)。 我总是得到 "Process finished with exit code 3" 并且没有其他任何事情发生。这是我用来尝试的代码(也在 Whosebug 中找到):

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class Model(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']

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

    def data(self, index, role):
        if not index.isValid(): return QVariant()
        elif role != Qt.DisplayRole:
            return QVariant()

        row=index.row()
        if row<len(self.items):
            return QVariant(self.items[row])
        else:
            return QVariant()

class Proxy(QSortFilterProxyModel):
    def __init__(self):
        super(Proxy, self).__init__()

    def filterAcceptsRow(self, row, parent):
        if '_B_' in self.sourceModel().data(self.sourceModel().index(row, 0), Qt.DisplayRole).toPyObject():
            return True
        return False

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        tableModel=Model(self)

        proxyModel=Proxy()
        proxyModel.setSourceModel(tableModel)

        self.tableview=QTableView(self)
        self.tableview.setModel(proxyModel)
        self.tableview.clicked.connect(self.viewClicked)
        self.tableview.horizontalHeader().setStretchLastSection(True)

        layout = QVBoxLayout(self)
        layout.addWidget(self.tableview)
        self.setLayout(layout)

    def viewClicked(self, indexClicked):
        print('index of proxy row', indexClicked.row())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

在此先感谢您的帮助。

在PyQt5中不再需要QVariant,数据直接转换为python的原生类型,因此QVariant不再存在方法toPyObject(),因此您必须将模型和代理更改为以下内容:

class Model(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']

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

    def columnCount(self, parent=QModelIndex()):
        return 1

    def data(self, index, role=Qt.DisplayRole):
        row = index.row()
        if 0 <= row < self.rowCount() and role == Qt.DisplayRole:
            return self.items[row]


class Proxy(QSortFilterProxyModel):
    def filterAcceptsRow(self, row, parent):
        return '_B_' in self.sourceModel().data(self.sourceModel().index(row, 0))

有关详细信息,请阅读 http://pyqt.sourceforge.net/Docs/PyQt5/pyqt_qvariant.html