diff options
author | Friedemann Kleint <[email protected]> | 2022-09-15 13:21:53 +0200 |
---|---|---|
committer | Friedemann Kleint <[email protected]> | 2022-09-16 10:30:36 +0200 |
commit | cf32b66adbfb489cd6e5d5c0bf3f741b59ba204c (patch) | |
tree | 44be69c9487f5d4db1092d061a555bd6001c1ab4 /examples/quick/models | |
parent | b20d6f6906f91f9df608d7800f4e27f7a7160abe (diff) |
Move examples around
Change the directory structure to closer match that of Qt.
Task-number: PYSIDE-841
Change-Id: I87aca346b6654aafe94dd1fb83c184c182ceb2e6
Reviewed-by: Qt CI Bot <[email protected]>
Reviewed-by: Cristian Maureira-Fredes <[email protected]>
Diffstat (limited to 'examples/quick/models')
-rw-r--r-- | examples/quick/models/objectlistmodel/doc/objectlistmodel.png | bin | 0 -> 1327 bytes | |||
-rw-r--r-- | examples/quick/models/objectlistmodel/doc/objectlistmodel.rst | 12 | ||||
-rw-r--r-- | examples/quick/models/objectlistmodel/objectlistmodel.py | 61 | ||||
-rw-r--r-- | examples/quick/models/objectlistmodel/objectlistmodel.pyproject | 3 | ||||
-rw-r--r-- | examples/quick/models/objectlistmodel/view.qml | 15 | ||||
-rw-r--r-- | examples/quick/models/stringlistmodel/doc/stringlistmodel.png | bin | 0 -> 1978 bytes | |||
-rw-r--r-- | examples/quick/models/stringlistmodel/doc/stringlistmodel.rst | 9 | ||||
-rw-r--r-- | examples/quick/models/stringlistmodel/stringlistmodel.py | 26 | ||||
-rw-r--r-- | examples/quick/models/stringlistmodel/stringlistmodel.pyproject | 3 | ||||
-rw-r--r-- | examples/quick/models/stringlistmodel/view.qml | 17 |
10 files changed, 146 insertions, 0 deletions
diff --git a/examples/quick/models/objectlistmodel/doc/objectlistmodel.png b/examples/quick/models/objectlistmodel/doc/objectlistmodel.png Binary files differnew file mode 100644 index 000000000..416e08a85 --- /dev/null +++ b/examples/quick/models/objectlistmodel/doc/objectlistmodel.png diff --git a/examples/quick/models/objectlistmodel/doc/objectlistmodel.rst b/examples/quick/models/objectlistmodel/doc/objectlistmodel.rst new file mode 100644 index 000000000..d71ee61df --- /dev/null +++ b/examples/quick/models/objectlistmodel/doc/objectlistmodel.rst @@ -0,0 +1,12 @@ +Object List Model Example +========================= + +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..0843ae480 --- /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 + nameChanged.emit() + + def color(self): + return self._color + + def setColor(self, color): + if color != self._color: + self._color = color + 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 Binary files differnew file mode 100644 index 000000000..eeb9b518a --- /dev/null +++ b/examples/quick/models/stringlistmodel/doc/stringlistmodel.png diff --git a/examples/quick/models/stringlistmodel/doc/stringlistmodel.rst b/examples/quick/models/stringlistmodel/doc/stringlistmodel.rst new file mode 100644 index 000000000..4c00ed130 --- /dev/null +++ b/examples/quick/models/stringlistmodel/doc/stringlistmodel.rst @@ -0,0 +1,9 @@ +String List Model Example +========================= + +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..a7a1807bb --- /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 } + } +} |