view DisplayModel.py @ 150:babe14449162

the entries for the selected feeds had to be set onto the item delegate so had access to the selected feed entry. Keep the list in one place only (the model) and access it from the item delegate.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sat, 27 Aug 2011 06:43:28 +0200
parents c5a427d46703
children bb3c851b18b1
line wrap: on
line source


from PyQt4.QtCore import QAbstractListModel, QModelIndex, QVariant, Qt

class DisplayModel(QAbstractListModel):
    def __init__(self, parent=None, list=None, displayAttribute=None, **args):
        QAbstractListModel.__init__(self, parent, *args)
        self.list = list
        self.displayAttribute = displayAttribute

    def rowCount(self, parent=QModelIndex()):
        return len(self.list)

    def data(self, index, role):
        if index.isValid() and role == Qt.DisplayRole:
            row = index.row()
            item = self.list[row]
            displayString = self._stringToDisplay(item)
            return QVariant(displayString)
        else:
            return QVariant()

    def _stringToDisplay(self, item):
        if hasattr(item, self.displayAttribute):
            return getattr(item, self.displayAttribute)
        else:
            return "invalid display attribute: " + self.displayAttribute