gpt4 book ai didi

python - QSortFilterProxyModel 创建空白项

转载 作者:行者123 更新时间:2023-12-01 08:16:33 25 4
gpt4 key购买 nike

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

What I want to happen:

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

You can see the empty lines expand the ListView and can even be selected by cursor

您可以看到空行展开了 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 然后两个 match ,在一般情况下,如果添加 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com