Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 0.75 KB  |  hits: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using QListView with a model defined in Pyside
  2. from PySide import QtCore, QtGui
  3.  
  4. class SimpleList(QtCore.QAbstractListModel):
  5.     def __init__(self, contents):
  6.         super(SimpleList, self).__init__()
  7.         self.contents = contents
  8.  
  9.     def rowCount(self, parent):
  10.         return len(self.contents)
  11.  
  12.     def data(self, index, role):
  13.         return str(self.contents[index.row()])
  14.  
  15.  
  16. app = QtGui.QApplication([])
  17. contents = SimpleList(["A", "B", "C"]) # In real code, these are complex objects
  18. simplelist = QtGui.QListView(None)
  19. simplelist.setGeometry(QtCore.QRect(0, 10, 791, 391))
  20. simplelist.setModel(contents)
  21. simplelist.show()
  22. app.exec_()
  23.        
  24. def data(self, index, role):
  25.     if role == QtCore.Qt.DisplayRole:
  26.         return str(self.contents[index.row()])