2012-02-17 10 views
4

私はPySideを使用して構築したリストを表示しようとしています。これは単に文字列のリスト(または私はQListWidgetを使用することができます)ではありませんが、例のために簡略化しました。Pysideで定義されたモデルでQListViewを使用する

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

私はnothingを参照してください。空のリストです。

私は間違っていますか?

答えて

3

あなたはrole引数チェックする必要があります:

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

をしかし、それはどのroleで動作QTableView、奇妙です。