4

PySide を使用して作成したリストを表示しようとしています。これは単なる文字列のリスト (または を使用できますQListWidget) ではありませんが、例として単純化しました。

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_()

空のリストだけです

私は何を間違っていますか?

4

1 に答える 1

3

role引数を確認する必要があります:

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

しかし、それは奇妙QTableViewですrole

于 2012-02-17T15:52:35.223 に答える