
Untitled
By: a guest on
Apr 28th, 2012 | syntax:
None | size: 0.75 KB | hits: 30 | expires: Never
Using QListView with a model defined in Pyside
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_()
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
return str(self.contents[index.row()])