aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2010-12-15 13:07:43 -0200
committerHugo Parente Lima <hugo.pl@gmail.com>2010-12-15 13:07:43 -0200
commit037443050c47afc198dcd10ae81543793227de80 (patch)
tree238dc268205ead76584f9057ab64fc93bfead1cf /examples
parente9386eae6aa9afb3ad53b3ef2d5275b39112834d (diff)
Add example for QML/QAbstractItemModel integration.
Diffstat (limited to 'examples')
-rw-r--r--examples/declarative/usingmodel.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/declarative/usingmodel.py b/examples/declarative/usingmodel.py
new file mode 100644
index 0000000..aeb1ad9
--- /dev/null
+++ b/examples/declarative/usingmodel.py
@@ -0,0 +1,49 @@
+import sys
+from PySide.QtCore import *
+from PySide.QtGui import *
+from PySide.QtDeclarative import *
+
+class PersonModel (QAbstractListModel):
+
+ MyRole = Qt.UserRole + 1
+
+ def __init__(self, parent = None):
+ QAbstractListModel.__init__(self, parent)
+ self.setRoleNames({
+ PersonModel.MyRole : 'modelData',
+ Qt.DisplayRole : 'display' # Qt asserts if you don't define a display role
+ })
+ self._data = []
+
+ def rowCount(self, index):
+ return len(self._data)
+
+ def data(self, index, role):
+ d = self._data[index.row()]
+
+ if role == Qt.DisplayRole:
+ return d['name']
+ elif role == Qt.DecorationRole:
+ return Qt.black
+ elif role == PersonModel.MyRole:
+ return d['myrole']
+ return None
+
+ def populate(self):
+ self._data.append({'name':'Qt', 'myrole':'role1'})
+ self._data.append({'name':'PySide', 'myrole':'role2'})
+
+if __name__ == '__main__':
+ app = QApplication(sys.argv)
+ view = QDeclarativeView()
+
+ myModel = PersonModel()
+ myModel.populate()
+
+ view.rootContext().setContextProperty("myModel", myModel)
+ view.setSource('view.qml')
+ view.show()
+
+ app.exec_()
+
+