diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-11-01 15:35:23 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-11-02 15:23:45 +0000 |
commit | fc658071c660ba4064278ef83e43f7d4ea18c319 (patch) | |
tree | 913c5dda0fc138d4a8846a47758946b6ff7e627d | |
parent | 3860ee7aef2407bb0bbc01e69e7d381f2c481d5d (diff) |
Add the 'properties' example of the QML reference examples
Task-number: PYSIDE-841
Change-Id: I6565bdbbc4ea401c027f6aab41598df48c779799
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit e057d9f4cbfd97e5fc0bdec564f63fabe5b50189)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
7 files changed, 381 insertions, 0 deletions
diff --git a/examples/declarative/referenceexamples/adding/doc/adding.rst b/examples/declarative/referenceexamples/adding/doc/adding.rst index b060f3c2c..55f6105b7 100644 --- a/examples/declarative/referenceexamples/adding/doc/adding.rst +++ b/examples/declarative/referenceexamples/adding/doc/adding.rst @@ -1,3 +1,5 @@ +.. _qml-adding-types-example: + Extending QML - Adding Types Example ==================================== diff --git a/examples/declarative/referenceexamples/properties/birthdayparty.py b/examples/declarative/referenceexamples/properties/birthdayparty.py new file mode 100644 index 000000000..9b4a8428d --- /dev/null +++ b/examples/declarative/referenceexamples/properties/birthdayparty.py @@ -0,0 +1,78 @@ +############################################################################# +## +## 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 PySide6.QtCore import QObject, Property +from PySide6.QtQml import QmlElement, ListProperty + +from person import Person + + +# To be used on the @QmlElement decorator +# (QML_IMPORT_MINOR_VERSION is optional) +QML_IMPORT_NAME = "examples.properties.people" +QML_IMPORT_MAJOR_VERSION = 1 + + +@QmlElement +class BirthdayParty(QObject): + + def __init__(self, parent=None): + super().__init__(parent) + self._host = None + self._guests = [] + + @Property(Person) + def host(self): + return self._host + + @host.setter + def host(self, h): + self._host = h + + def guest(self, n): + return self._guests[n] + + def guestCount(self): + return len(self._guests) + + def appendGuest(self, guest): + self._guests.append(guest) + + guests = ListProperty(Person, appendGuest) diff --git a/examples/declarative/referenceexamples/properties/doc/properties.rst b/examples/declarative/referenceexamples/properties/doc/properties.rst new file mode 100644 index 000000000..86c0ef96f --- /dev/null +++ b/examples/declarative/referenceexamples/properties/doc/properties.rst @@ -0,0 +1,89 @@ +_qml-object-and-list-property-types-example: + +Extending QML - Object and List Property Types Example +====================================================== + +Exporting C++ Properties. + +This example builds on :ref:`qml-adding-types-example`. + +The Object and List Property Types example shows how to add object and list +properties in QML. This example adds a BirthdayParty type that specifies a +birthday party, consisting of a celebrant and a list of guests. People are +specified using the People QML type built in the previous example. + +import examples.properties.people + +.. code-block:: javascript: + + BirthdayParty { + host: Person { + name: "Bob Jones" + shoe_size: 12 + } + guests: [ + Person { name: "Leo Hodges" }, + Person { name: "Jack Smith" }, + Person { name: "Anne Brown" } + ] + } + +Declare the BirthdayParty +------------------------- + +The BirthdayParty class is declared like this: + +.. code-block:: python: + + from person import Person + + + # To be used on the @QmlElement decorator + # (QML_IMPORT_MINOR_VERSION is optional) + QML_IMPORT_NAME = "examples.properties.people" + QML_IMPORT_MAJOR_VERSION = 1 + + + @QmlElement + class BirthdayParty(QObject): + + def __init__(self, parent=None): + super().__init__(parent) + self._host = None + self._guests = [] + + @Property(Person) + def host(self): + return self._host + + @host.setter + def host(self, h): + self._host = h + + def guest(self, n): + return self._guests[n] + + def guestCount(self): + return len(self._guests) + + def appendGuest(self, guest): + self._guests.append(guest) + + guests = ListProperty(Person, appendGuest) + +The class contains a member to store the celebrant object, and also a +list member storing the Person instances. + +In QML, the type of a list properties - and the guests property is a list of +people - are all of type ListProperty. ListProperty is simple value +type that contains a set of functions. QML calls these functions +whenever it needs to read from, write to or otherwise interact with +the list. In addition to concrete lists like the people list used in this +example, the use of QQmlListProperty allows for "virtual lists" and other advanced +scenarios. + +Running the Example +------------------- + +The main.py file in the example includes a simple shell application that +loads and runs the QML snippet shown at the beginning of this page. diff --git a/examples/declarative/referenceexamples/properties/example.qml b/examples/declarative/referenceexamples/properties/example.qml new file mode 100644 index 000000000..7e9f42bbf --- /dev/null +++ b/examples/declarative/referenceexamples/properties/example.qml @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, 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 examples.properties.people + +BirthdayParty { + host: Person { + name: "Bob Jones" + shoe_size: 12 + } + guests: [ + Person { name: "Leo Hodges" }, + Person { name: "Jack Smith" }, + Person { name: "Anne Brown" } + ] +} diff --git a/examples/declarative/referenceexamples/properties/main.py b/examples/declarative/referenceexamples/properties/main.py new file mode 100644 index 000000000..d26747383 --- /dev/null +++ b/examples/declarative/referenceexamples/properties/main.py @@ -0,0 +1,71 @@ +############################################################################# +## +## 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$ +## +############################################################################# + +"""PySide6 port of the qml/examples/qml/referenceexamples/properties example from Qt v6.x""" + +from pathlib import Path +import sys + +from PySide6.QtCore import QCoreApplication, QUrl +from PySide6.QtQml import QQmlComponent, QQmlEngine + +from person import Person +from birthdayparty import BirthdayParty + + +if __name__ == '__main__': + app = QCoreApplication(sys.argv) + + qml_file = Path(__file__).parent / "example.qml" + url = QUrl.fromLocalFile(qml_file) + engine = QQmlEngine() + component = QQmlComponent(engine, url) + + party = component.create() + if party: + print(f"{party.host} is having a birthday!\nThey are inviting:") + for g in range(party.guestCount()): + name = party.guest(g).name + print(f" {name}") + else: + print(component.errors()) + + del engine + sys.exit(0) diff --git a/examples/declarative/referenceexamples/properties/person.py b/examples/declarative/referenceexamples/properties/person.py new file mode 100644 index 000000000..e08d9ae90 --- /dev/null +++ b/examples/declarative/referenceexamples/properties/person.py @@ -0,0 +1,72 @@ +############################################################################# +## +## 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 PySide6.QtCore import QObject, Property +from PySide6.QtQml import QmlElement + +# To be used on the @QmlElement decorator +# (QML_IMPORT_MINOR_VERSION is optional) +QML_IMPORT_NAME = "examples.properties.people" +QML_IMPORT_MAJOR_VERSION = 1 + + +@QmlElement +class Person(QObject): + def __init__(self, parent=None): + super().__init__(parent) + self._name = '' + self._shoe_size = 0 + + @Property(str) + def name(self): + return self._name + + @name.setter + def name(self, n): + self._name = n + + @Property(int) + def shoe_size(self): + return self._shoe_size + + @shoe_size.setter + def shoe_size(self, s): + self._shoe_size = s + diff --git a/examples/declarative/referenceexamples/properties/properties.pyproject b/examples/declarative/referenceexamples/properties/properties.pyproject new file mode 100644 index 000000000..0f5958fc3 --- /dev/null +++ b/examples/declarative/referenceexamples/properties/properties.pyproject @@ -0,0 +1,6 @@ +{ + "files": ["example.qml", + "main.py", + "person.py", + "birthdayparty.py"] +} |