我想要做的是:从模型中取出项目并使用排序代理按不同的角色对它们进行排序:预期输出:
实际输出包含不应该存在的空行:
您可以看到空行扩展了 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_()
慕无忌1623718
相关分类