Frage

I'm having some problem sorting the items in my QListView using values in a field I specified.

Basically what I'm trying to do is this:

  1. Detect faces in a collection of photos and display them in a QListView
  2. Cluster the faces (images)
  3. Update the view by placing items in the list (which are face images) belonging to the same cluster in together. Concretely, if item 1, 3, 5 are in one cluster and items 2, 4, 6 are in another, then items 1, 3, 5 should be displayed (in whatever permutations) before any of items 2, 4, 6 are displayed or vice versa.

The way I went about doing this is to set one of the UserRole field for each QStandardItem in my list to the cluster label and then try to get the QStandardModel to sort according to this UserRole. This would then display items in the same cluster (i.e. with the same cluster label in the UserRole) next to each other.

I'm able to set the UserRole successfully for the items but calling the sort function on the QStandardModel did not sort the items even though when I set the sort role to be the default DisplayRole (i.e. sort according to the text label of each face) it worked as intended.

Can anyone tell me what is wrong with my code or offer an alternative method? I've googled sorting list and I found the following link on QSortFilterProxyModel but as I'm quite new to Qt, I'm not able to adapt it to my situation.

Thanks in advance to any replies.

Here is the relevant code:

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()
War es hilfreich?

Lösung

There's nothing wrong with the code you posted. So there must be something wrong with how you are using it. How are you generating the cluster labels?

Here's a test script using your FacesView class that sorts as you intended:

from random import randint
from PySide.QtGui import QWidget, QPushButton, QVBoxLayout, QApplication
from facesview import FacesView

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.list = FacesView(self)
        self.button = QPushButton('Test', self)
        self.button.clicked.connect(self.handleButton)
        layout = QVBoxLayout(self)
        layout.addWidget(self.list)
        layout.addWidget(self.button)

    def handleButton(self):
        labels = []
        self.list.model().setRowCount(0)
        for row in range(10):
            labels.append(randint(0, 3))
            text = 'Item(%d) - Cluster(%d)' % (row, labels[-1])
            self.list.addItem(text, 'icon.png')
        self.list.updateFaceClusters(labels)

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top