aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick/models
diff options
context:
space:
mode:
Diffstat (limited to 'examples/quick/models')
-rw-r--r--examples/quick/models/objectlistmodel/doc/objectlistmodel.pngbin0 -> 1327 bytes
-rw-r--r--examples/quick/models/objectlistmodel/doc/objectlistmodel.rst14
-rw-r--r--examples/quick/models/objectlistmodel/objectlistmodel.py61
-rw-r--r--examples/quick/models/objectlistmodel/objectlistmodel.pyproject3
-rw-r--r--examples/quick/models/objectlistmodel/view.qml15
-rw-r--r--examples/quick/models/stringlistmodel/doc/stringlistmodel.pngbin0 -> 1978 bytes
-rw-r--r--examples/quick/models/stringlistmodel/doc/stringlistmodel.rst11
-rw-r--r--examples/quick/models/stringlistmodel/stringlistmodel.py26
-rw-r--r--examples/quick/models/stringlistmodel/stringlistmodel.pyproject3
-rw-r--r--examples/quick/models/stringlistmodel/view.qml17
10 files changed, 150 insertions, 0 deletions
diff --git a/examples/quick/models/objectlistmodel/doc/objectlistmodel.png b/examples/quick/models/objectlistmodel/doc/objectlistmodel.png
new file mode 100644
index 000000000..416e08a85
--- /dev/null
+++ b/examples/quick/models/objectlistmodel/doc/objectlistmodel.png
Binary files differ
diff --git a/examples/quick/models/objectlistmodel/doc/objectlistmodel.rst b/examples/quick/models/objectlistmodel/doc/objectlistmodel.rst
new file mode 100644
index 000000000..a4af62706
--- /dev/null
+++ b/examples/quick/models/objectlistmodel/doc/objectlistmodel.rst
@@ -0,0 +1,14 @@
+Object List Model Example
+=========================
+
+.. tags:: Android
+
+A list of QObject values can also be used as a model.
+A list[QObject,] provides the properties of the objects in the list as roles.
+
+The following application creates a DataObject class with Property values
+that will be accessible as named roles when a list[DataObject,] is exposed to QML:
+
+.. image:: objectlistmodel.png
+ :width: 400
+ :alt: Object List Model Screenshot
diff --git a/examples/quick/models/objectlistmodel/objectlistmodel.py b/examples/quick/models/objectlistmodel/objectlistmodel.py
new file mode 100644
index 000000000..968761e5c
--- /dev/null
+++ b/examples/quick/models/objectlistmodel/objectlistmodel.py
@@ -0,0 +1,61 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+from pathlib import Path
+import sys
+from PySide6.QtCore import QObject, QUrl, Property, Signal
+from PySide6.QtGui import QGuiApplication
+from PySide6.QtQuick import QQuickView
+
+# This example illustrates exposing a list of QObjects as a model in QML
+
+
+class DataObject(QObject):
+
+ nameChanged = Signal()
+ colorChanged = Signal()
+
+ def __init__(self, name, color, parent=None):
+ super().__init__(parent)
+ self._name = name
+ self._color = color
+
+ def name(self):
+ return self._name
+
+ def setName(self, name):
+ if name != self._name:
+ self._name = name
+ self.nameChanged.emit()
+
+ def color(self):
+ return self._color
+
+ def setColor(self, color):
+ if color != self._color:
+ self._color = color
+ self.colorChanged.emit()
+
+ name = Property(str, name, setName, notify=nameChanged)
+ color = Property(str, color, setColor, notify=colorChanged)
+
+
+if __name__ == '__main__':
+ app = QGuiApplication(sys.argv)
+
+ dataList = [DataObject("Item 1", "red"),
+ DataObject("Item 2", "green"),
+ DataObject("Item 3", "blue"),
+ DataObject("Item 4", "yellow")]
+
+ view = QQuickView()
+ view.setResizeMode(QQuickView.SizeRootObjectToView)
+ view.setInitialProperties({"model": dataList})
+
+ qml_file = Path(__file__).parent / "view.qml"
+ view.setSource(QUrl.fromLocalFile(qml_file))
+ view.show()
+
+ r = app.exec()
+ del view
+ sys.exit(r)
diff --git a/examples/quick/models/objectlistmodel/objectlistmodel.pyproject b/examples/quick/models/objectlistmodel/objectlistmodel.pyproject
new file mode 100644
index 000000000..556e399f4
--- /dev/null
+++ b/examples/quick/models/objectlistmodel/objectlistmodel.pyproject
@@ -0,0 +1,3 @@
+{
+ "files": ["objectlistmodel.py", "view.qml"]
+}
diff --git a/examples/quick/models/objectlistmodel/view.qml b/examples/quick/models/objectlistmodel/view.qml
new file mode 100644
index 000000000..b7cf68a9b
--- /dev/null
+++ b/examples/quick/models/objectlistmodel/view.qml
@@ -0,0 +1,15 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+import QtQuick
+
+ListView {
+ width: 100; height: 100
+
+ delegate: Rectangle {
+ color: model.modelData.color
+ height: 25
+ width: 100
+ Text { text: model.modelData.name }
+ }
+}
diff --git a/examples/quick/models/stringlistmodel/doc/stringlistmodel.png b/examples/quick/models/stringlistmodel/doc/stringlistmodel.png
new file mode 100644
index 000000000..eeb9b518a
--- /dev/null
+++ b/examples/quick/models/stringlistmodel/doc/stringlistmodel.png
Binary files differ
diff --git a/examples/quick/models/stringlistmodel/doc/stringlistmodel.rst b/examples/quick/models/stringlistmodel/doc/stringlistmodel.rst
new file mode 100644
index 000000000..ce11674b4
--- /dev/null
+++ b/examples/quick/models/stringlistmodel/doc/stringlistmodel.rst
@@ -0,0 +1,11 @@
+String List Model Example
+=========================
+
+.. tags:: Android
+
+A model may be a simple 'list',
+which provides the contents of the list via the modelData role.
+
+.. image:: stringlistmodel.png
+ :width: 400
+ :alt: String List Model Screenshot
diff --git a/examples/quick/models/stringlistmodel/stringlistmodel.py b/examples/quick/models/stringlistmodel/stringlistmodel.py
new file mode 100644
index 000000000..3982b1ffc
--- /dev/null
+++ b/examples/quick/models/stringlistmodel/stringlistmodel.py
@@ -0,0 +1,26 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+from pathlib import Path
+import sys
+from PySide6.QtCore import QUrl
+from PySide6.QtGui import QGuiApplication
+from PySide6.QtQuick import QQuickView
+
+# This example illustrates exposing a QStringList as a model in QML
+
+if __name__ == '__main__':
+ app = QGuiApplication(sys.argv)
+
+ dataList = ["Item 1", "Item 2", "Item 3", "Item 4"]
+
+ view = QQuickView()
+ view.setInitialProperties({"model": dataList})
+
+ qml_file = Path(__file__).parent / "view.qml"
+ view.setSource(QUrl.fromLocalFile(qml_file))
+ view.show()
+
+ r = app.exec()
+ del view
+ sys.exit(r)
diff --git a/examples/quick/models/stringlistmodel/stringlistmodel.pyproject b/examples/quick/models/stringlistmodel/stringlistmodel.pyproject
new file mode 100644
index 000000000..5ec3e85d1
--- /dev/null
+++ b/examples/quick/models/stringlistmodel/stringlistmodel.pyproject
@@ -0,0 +1,3 @@
+{
+ "files": ["stringlistmodel.py", "view.qml"]
+}
diff --git a/examples/quick/models/stringlistmodel/view.qml b/examples/quick/models/stringlistmodel/view.qml
new file mode 100644
index 000000000..5db5576f4
--- /dev/null
+++ b/examples/quick/models/stringlistmodel/view.qml
@@ -0,0 +1,17 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+import QtQuick
+
+ListView {
+ width: 100
+ height: 100
+ required model
+
+ delegate: Rectangle {
+ required property string modelData
+ height: 25
+ width: 100
+ Text { text: parent.modelData }
+ }
+}