diff options
author | Cristián Maureira-Fredes <Cristian.Maureira-Fredes@qt.io> | 2021-07-13 22:51:34 +0200 |
---|---|---|
committer | Cristián Maureira-Fredes <Cristian.Maureira-Fredes@qt.io> | 2021-07-20 16:56:48 +0200 |
commit | cd2c32050705d25e648bf8e015b3a1adf4134b05 (patch) | |
tree | a1f9699732c91b7ac60645680e621b4983ba335c /examples/declarative | |
parent | 3a1b617e5bfbdd077a51c7d57f88cb58f513b780 (diff) |
examples: port string and object list models
Task-number: PYSIDE-841
Change-Id: Iec9843e0aff8fc02107c7899a0e56f067c5a6936
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Keith Kyzivat <keith.kyzivat@qt.io>
Diffstat (limited to 'examples/declarative')
-rw-r--r-- | examples/declarative/objectlistmodel/doc/objectlistmodel.png | bin | 0 -> 1327 bytes | |||
-rw-r--r-- | examples/declarative/objectlistmodel/doc/objectlistmodel.rst | 12 | ||||
-rw-r--r-- | examples/declarative/objectlistmodel/objectlistmodel.py | 99 | ||||
-rw-r--r-- | examples/declarative/objectlistmodel/objectlistmodel.pyproject | 3 | ||||
-rw-r--r-- | examples/declarative/objectlistmodel/view.qml | 52 | ||||
-rw-r--r-- | examples/declarative/stringlistmodel/doc/stringlistmodel.png | bin | 0 -> 79 bytes | |||
-rw-r--r-- | examples/declarative/stringlistmodel/doc/stringlistmodel.rst | 9 | ||||
-rw-r--r-- | examples/declarative/stringlistmodel/stringlistmodel.py | 64 | ||||
-rw-r--r-- | examples/declarative/stringlistmodel/stringlistmodel.pyproject | 3 | ||||
-rw-r--r-- | examples/declarative/stringlistmodel/view.qml | 54 |
10 files changed, 296 insertions, 0 deletions
diff --git a/examples/declarative/objectlistmodel/doc/objectlistmodel.png b/examples/declarative/objectlistmodel/doc/objectlistmodel.png Binary files differnew file mode 100644 index 000000000..416e08a85 --- /dev/null +++ b/examples/declarative/objectlistmodel/doc/objectlistmodel.png diff --git a/examples/declarative/objectlistmodel/doc/objectlistmodel.rst b/examples/declarative/objectlistmodel/doc/objectlistmodel.rst new file mode 100644 index 000000000..d71ee61df --- /dev/null +++ b/examples/declarative/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/declarative/objectlistmodel/objectlistmodel.py b/examples/declarative/objectlistmodel/objectlistmodel.py new file mode 100644 index 000000000..0a3d5501e --- /dev/null +++ b/examples/declarative/objectlistmodel/objectlistmodel.py @@ -0,0 +1,99 @@ + +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +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/declarative/objectlistmodel/objectlistmodel.pyproject b/examples/declarative/objectlistmodel/objectlistmodel.pyproject new file mode 100644 index 000000000..556e399f4 --- /dev/null +++ b/examples/declarative/objectlistmodel/objectlistmodel.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["objectlistmodel.py", "view.qml"] +} diff --git a/examples/declarative/objectlistmodel/view.qml b/examples/declarative/objectlistmodel/view.qml new file mode 100644 index 000000000..d5012a058 --- /dev/null +++ b/examples/declarative/objectlistmodel/view.qml @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt for Python examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +ListView { + width: 100; height: 100 + + delegate: Rectangle { + color: model.modelData.color + height: 25 + width: 100 + Text { text: model.modelData.name } + } +} diff --git a/examples/declarative/stringlistmodel/doc/stringlistmodel.png b/examples/declarative/stringlistmodel/doc/stringlistmodel.png Binary files differnew file mode 100644 index 000000000..6058c5930 --- /dev/null +++ b/examples/declarative/stringlistmodel/doc/stringlistmodel.png diff --git a/examples/declarative/stringlistmodel/doc/stringlistmodel.rst b/examples/declarative/stringlistmodel/doc/stringlistmodel.rst new file mode 100644 index 000000000..4c00ed130 --- /dev/null +++ b/examples/declarative/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/declarative/stringlistmodel/stringlistmodel.py b/examples/declarative/stringlistmodel/stringlistmodel.py new file mode 100644 index 000000000..4e5e54466 --- /dev/null +++ b/examples/declarative/stringlistmodel/stringlistmodel.py @@ -0,0 +1,64 @@ + +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +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/declarative/stringlistmodel/stringlistmodel.pyproject b/examples/declarative/stringlistmodel/stringlistmodel.pyproject new file mode 100644 index 000000000..5ec3e85d1 --- /dev/null +++ b/examples/declarative/stringlistmodel/stringlistmodel.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["stringlistmodel.py", "view.qml"] +} diff --git a/examples/declarative/stringlistmodel/view.qml b/examples/declarative/stringlistmodel/view.qml new file mode 100644 index 000000000..0e0bbd0e5 --- /dev/null +++ b/examples/declarative/stringlistmodel/view.qml @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt for Python examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +ListView { + width: 100 + height: 100 + required model + + delegate: Rectangle { + required property string modelData + height: 25 + width: 100 + Text { text: parent.modelData } + } +} |