我要执行的操作:从模型中获取项目,然后使用排序代理以不同的角色对它们进行排序:
预期产量:

python - QSortFilterProxyModel创建空白项-LMLPHP

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

python - QSortFilterProxyModel创建空白项-LMLPHP

您可以看到空行会展开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,则必须将行号从其添加到的位置以及从该位置传递到添加的位置,在这种情况下,仅添加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()

关于python - QSortFilterProxyModel创建空白项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54956844/

10-11 19:07