我在对QListView
我指定的字段中的使用值中的项目进行排序时遇到了一些问题。
基本上我想要做的是:
- 检测照片集合中的人脸并将其显示在
QListView
- 聚类人脸(图像)
- 通过将列表中属于同一集群的项目(它们是面部图像)放在一起来更新视图。具体来说,如果项目 1、3、5 在一个集群中,而项目 2、4、6 在另一个集群中,则项目 1、3、5 应该在显示项目 2、4、6 之前显示(以任何排列方式)或相反亦然。
我这样做的方法是将列表中的UserRole
每个字段设置为QStandardItem
集群标签,然后尝试QStandardModel
根据 this 进行排序UserRole
。然后,这将显示同一集群中的项目(即在 中具有相同的集群标签UserRole
)彼此相邻。
我能够为UserRole
项目设置成功,但调用排序函数QStandardModel
并没有对项目进行排序,即使我将排序角色设置为默认值DisplayRole
(即根据每个面的文本标签排序)它的工作方式为故意的。
谁能告诉我我的代码有什么问题或提供替代方法?我用谷歌搜索了排序列表,并在QSortFilterProxyModel上找到了以下链接,但由于我对 Qt 很陌生,因此无法适应我的情况。
提前感谢任何回复。
以下是相关代码:
import os
from PySide.QtGui import QListView, QStandardItemModel, QStandardItem, QIcon
from PySide.QtCore import Qt
class FacesView(QListView):
"""
View to display detected faces for user to see and label.
"""
UNCLUSTERED_LABEL = -1
CLUSTER_ROLE = Qt.UserRole + 1
def __init__(self, *args):
super(FacesView, self).__init__(*args)
self._dataModel = QStandardItemModel()
self.setModel(self._dataModel)
# Layout items in batches instead of waiting for all items to be
# loaded before user is allowed to interact with them.
self.setLayoutMode(QListView.Batched)
def updateFaceClusters(self, labels):
"""Update the cluster label for each face.
@param labels: [1 x N] array where each element is an integer
for the cluster the face belongs to."""
assert(len(labels) == self._dataModel.rowCount())
# Put the cluster label each item/face belong to in the
# CLUSTER_ROLE field.
for i in xrange(self._dataModel.rowCount()):
index = self._dataModel.index(i, 0)
self._dataModel.setData(index, labels[i], self.CLUSTER_ROLE)
# Use cluster label as sort role
self._dataModel.setSortRole(self.CLUSTER_ROLE)
# This does NOT seem to sort the items even though it works fine
# when sort role is the default Qt.DisplayRole.
self._dataModel.sort(0)
print("Finished updating face clusters")
def itemsInList(self):
"""Returns the label for a face and the path to its image.
@return: (label, path)"""
items = []
for i in xrange(self._dataModel.rowCount()):
label = self._dataModel.index(i, 0).data(Qt.DisplayRole)
imagePath = self._dataModel.index(i, 0).data(Qt.UserRole)
clusterLabel = self._dataModel.index(i, 0).data(self.CLUSTER_ROLE)
items.append((imagePath, label, clusterLabel))
return items
def addItem(self, label, imagePath):
"""Add an item to list view
@param label: The label associated with the item.
@param imagePath: Path to image for the icon."""
if os.path.exists(imagePath):
icon = QIcon(imagePath)
else:
icon = QIcon(':/res/Unknown-person.gif')
item = QStandardItem(icon, label)
item.setEditable(True)
# Add image path to the UserRole field.
item.setData(imagePath, Qt.UserRole)
# Add cluster label to image. CLUSTER_ROLE is where I intend
# to put the item's cluster label.
item.setData(self.UNCLUSTERED_LABEL, self.CLUSTER_ROLE)
# Prevent an item from dropping into another item.
item.setDropEnabled(False)
# Add item to list indirectly by adding it to the model.
self._dataModel.appendRow(item)
def clear(self):
self._dataModel.clear()