Guest User

PySide sample

a guest
Feb 18th, 2011
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. import sys
  2. from PySide.QtCore import *
  3. from PySide.QtGui import *
  4. from PySide.QtDeclarative import *
  5.  
  6. class TestAList(QAbstractListModel):
  7.     def __init__(self,parent = None):
  8.         QAbstractListModel.__init__(self,parent)
  9.         # Set some roles
  10.         self.roles = dict()
  11.         self.roles[Qt.UserRole+1] = 'key1'
  12.         self.roles[Qt.UserRole+2] = 'key2'
  13.         self.roles[Qt.DisplayRole] = 'display'
  14.         self.setRoleNames(self.roles)
  15.        
  16.     def rowCount(self, parent=QModelIndex()):
  17.         return 3
  18.     #def columnCount(self, parent=QModelIndex()):
  19.     #    return 1
  20.    
  21.     def data(self, index, role):
  22.         if index.isValid() and role == Qt.UserRole+1:
  23.             return "hello(from abstractlist!)"
  24.         elif index.isValid() and role == Qt.UserRole+2:
  25.             return TestAList()
  26.         elif index.isValid() and role == Qt.DisplayRole:
  27.             return "display"
  28.         return None
  29.        
  30.  
  31. class TestItem(QObject):
  32.     dataChanged = Signal()
  33.     def getAbstractList(self):
  34.         if not hasattr(self,'_alist'):
  35.             self._alist = TestAList()
  36.         return self._alist
  37.    
  38.     def getNormalList(self):
  39.         if not hasattr(self,'_nlist'):
  40.             d = dict()
  41.             d['key1'] = 'hello from normal list' # Simple string value
  42.             d['child'] = [1,2,3,4] # list value.
  43.             self._nlist = [d, d, d]
  44.         return self._nlist  # return a list of these dict objects
  45.     alist = Property(QObject,  getAbstractList, notify=dataChanged)
  46.     plist = Property(list, getNormalList, notify=dataChanged)
  47.    
  48.     def __init__(self, parent = None):
  49.         QObject.__init__(self, parent)
  50.         print "INIT FIN"
  51.        
  52.        
  53. if __name__ == '__main__':
  54.     app = QApplication(sys.argv)
  55.     view = QDeclarativeView()
  56.    
  57.     tobject = TestItem()
  58.     view.rootContext().setContextProperty("pythonQObject", tobject)
  59.     view.setSource('list-test.qml')
  60.     view.show()
  61.     app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment