Frage

I have been trying to display a list that I build using PySide. It is not simply a list of strings (or I could use QListWidget), but I simplified it for the example.

from PySide import QtCore, QtGui

class SimpleList(QtCore.QAbstractListModel):
    def __init__(self, contents):
        super(SimpleList, self).__init__()
        self.contents = contents

    def rowCount(self, parent):
        return len(self.contents)

    def data(self, index, role):
        return str(self.contents[index.row()])


app = QtGui.QApplication([])
contents = SimpleList(["A", "B", "C"]) # In real code, these are complex objects
simplelist = QtGui.QListView(None)
simplelist.setGeometry(QtCore.QRect(0, 10, 791, 391))
simplelist.setModel(contents)
simplelist.show()
app.exec_()

I see nothing, just an empty list.

What am I doing wrong?

War es hilfreich?

Lösung

You should check role argument:

def data(self, index, role):
    if role == QtCore.Qt.DisplayRole:
        return str(self.contents[index.row()])

But it's strange, QTableView works with any role.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top