QSortFilterProxyModel 创建空白项

QSortFilterProxyModel creates blank items

我正在尝试做的事情:从模型中取出项目并使用排序代理按不同角色对它们进行排序: 预期输出:

实际输出包含不应该存在的空行:

你可以看到空行扩展了ListView,甚至可以被光标选中。

这是产生这种不正确行为的代码:

from PySide2.QtCore import *
from PySide2.QtWidgets import *
import sys
import string
import random

class MyItem:
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def __str__(self):
        return self.name +" "+ str(self.value)

class MyCustomModel(QAbstractListModel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.list = []

    def rowCount(self, parent=None):
        return len(self.list)

    def data(self, index, role):
        row = index.row()
        if row < 0 or row >= len(self.list):
            return None

        item = self.list[row]
        if role == Qt.DisplayRole:
            return str(item)
        if role == Qt.UserRole:
            return item.value
        else:
            return None

    def add(self, item):
        rc = self.rowCount()
        self.beginInsertRows(QModelIndex(), rc, rc+1)
        self.list.append(item)
        self.endInsertRows()

class MyWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.model = MyCustomModel()
        self.listView = QListView(self)

        self.sortingProxy = QSortFilterProxyModel()
        self.sortingProxy.setSourceModel(self.model)
        self.sortingProxy.setSortRole(Qt.UserRole)
        self.sortingProxy.sort(0, Qt.AscendingOrder)

        self.listView.setModel(self.sortingProxy)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.listView)

        self.setLayout(self.layout)
        self.show()

        # create some random data for the model
        for i in range(10):
            randomName = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(8)])
            self.model.add(MyItem(randomName, random.randint(0, 30)))

app = QApplication(sys.argv)
widget = MyWidget()
app.exec_()

我已将问题追查到 QSortFilterProxyModel,因为当它被删除时问题就消失了,但程序不再对数据进行排序:

from PySide2.QtCore import *
from PySide2.QtWidgets import *
import sys
import string
import random

class MyItem:
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def __str__(self):
        return self.name +" "+ str(self.value)

class MyCustomModel(QAbstractListModel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.list = []

    def rowCount(self, parent=None):
        return len(self.list)

    def data(self, index, role):
        row = index.row()
        if row < 0 or row >= len(self.list):
            return None

        item = self.list[row]
        if role == Qt.DisplayRole:
            return str(item)
        if role == Qt.UserRole:
            return item.value
        else:
            return None

    def add(self, item):
        rc = self.rowCount()
        self.beginInsertRows(QModelIndex(), rc, rc+1)
        self.list.append(item)
        self.endInsertRows()

class MyWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.model = MyCustomModel()
        self.listView = QListView(self)

        self.listView.setModel(self.model)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.listView)

        self.setLayout(self.layout)
        self.show()

        # create some random data for the model
        for i in range(10):
            randomName = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(8)])
            self.model.add(MyItem(randomName, random.randint(0, 30)))

app = QApplication(sys.argv)
widget = MyWidget()
app.exec_()

因为问题似乎是由 Pyside2/Qt5 代码引起的,所以我似乎不知道如何解决它。

问题不在于代理,问题是由您用来添加项目的方法引起的,如果您查看 docs 您必须将行号从添加的位置传递到添加的位置,在此case 只加 1 然后都匹配,一般情况下如果加 n 个元素,解是:

rc = self.rowCount()
self.beginInsertRows(QModelIndex(), rc, rc + n - 1)

所以在你的情况下,解决方案是:

def add(self, item):
    rc = self.rowCount()
    self.beginInsertRows(QModelIndex(), rc, rc)
    self.list.append(item)
    self.endInsertRows()