aboutsummaryrefslogtreecommitdiffstats
path: root/examples/declarative
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-11-05 10:57:23 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-11-10 22:43:40 +0100
commit20fee1eb56f64ecc6d356abe658da14d09e7d09a (patch)
treedb0be9386e348e7007ce6854abb0f3bc2ddc5e89 /examples/declarative
parent4f57d4eb32df52838129547be60b1a75d3f3356c (diff)
Add the 'coercion' example of the QML reference examples
Task-number: PYSIDE-841 Pick-to: 6.2 Change-Id: Iff14a58f065070ef3ffe409bb1516bd4ed30dac7 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'examples/declarative')
-rw-r--r--examples/declarative/referenceexamples/coercion/birthdayparty.py78
-rw-r--r--examples/declarative/referenceexamples/coercion/coercion.pyproject3
-rw-r--r--examples/declarative/referenceexamples/coercion/doc/coercion.rst35
-rw-r--r--examples/declarative/referenceexamples/coercion/example.qml63
-rw-r--r--examples/declarative/referenceexamples/coercion/main.py73
-rw-r--r--examples/declarative/referenceexamples/coercion/person.py84
-rw-r--r--examples/declarative/referenceexamples/properties/doc/properties.rst2
7 files changed, 337 insertions, 1 deletions
diff --git a/examples/declarative/referenceexamples/coercion/birthdayparty.py b/examples/declarative/referenceexamples/coercion/birthdayparty.py
new file mode 100644
index 000000000..4db44d703
--- /dev/null
+++ b/examples/declarative/referenceexamples/coercion/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.coercion.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/coercion/coercion.pyproject b/examples/declarative/referenceexamples/coercion/coercion.pyproject
new file mode 100644
index 000000000..3c01c40c2
--- /dev/null
+++ b/examples/declarative/referenceexamples/coercion/coercion.pyproject
@@ -0,0 +1,3 @@
+{
+ "files": ["main.py", "birthdayparty.py", "person.py", "example.qml"]
+}
diff --git a/examples/declarative/referenceexamples/coercion/doc/coercion.rst b/examples/declarative/referenceexamples/coercion/doc/coercion.rst
new file mode 100644
index 000000000..2ccdaeb4f
--- /dev/null
+++ b/examples/declarative/referenceexamples/coercion/doc/coercion.rst
@@ -0,0 +1,35 @@
+.. _qml-inheritance-and-coercion-example:
+
+Extending QML - Inheritance and Coercion Example
+================================================
+
+This example builds on the :ref:`qml-adding-types-example` and the
+:ref:`qml-object-and-list-property-types-example` .
+
+The Inheritance and Coercion Example shows how to use base classes to assign
+types of more than one type to a property. It specializes the Person type
+developed in the previous examples into two types - a ``Boy`` and a ``Girl``.
+
+Declare Boy and Girl
+--------------------
+
+The Person class remains unaltered in this example and the Boy and Girl C++
+classes are trivial extensions of it. The types and their QML name are
+registered with the QML engine.
+
+As an example, the inheritance used here is a little contrived, but in real
+applications it is likely that the two extensions would add additional
+properties or modify the Person classes behavior.
+
+Running the Example
+-------------------
+
+The BirthdayParty type has not changed since the previous example. The
+celebrant and guests property still use the People type.
+
+However, as all three types, Person, Boy and Girl, have been registered with the
+QML system, on assignment QML automatically (and type-safely) converts the Boy
+and Girl objects into a Person.
+
+The main.py file in the example includes a simple shell application that
+loads and runs the QML snippet shown below.
diff --git a/examples/declarative/referenceexamples/coercion/example.qml b/examples/declarative/referenceexamples/coercion/example.qml
new file mode 100644
index 000000000..919fc36c7
--- /dev/null
+++ b/examples/declarative/referenceexamples/coercion/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 Qt for Python 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.coercion.people
+
+BirthdayParty {
+ host: Boy {
+ name: "Bob Jones"
+ shoe_size: 12
+ }
+ guests: [
+ Boy { name: "Leo Hodges" },
+ Boy { name: "Jack Smith" },
+ Girl { name: "Anne Brown" }
+ ]
+}
diff --git a/examples/declarative/referenceexamples/coercion/main.py b/examples/declarative/referenceexamples/coercion/main.py
new file mode 100644
index 000000000..fc945e184
--- /dev/null
+++ b/examples/declarative/referenceexamples/coercion/main.py
@@ -0,0 +1,73 @@
+#############################################################################
+##
+## 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/coercion 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 Boy, Girl
+from birthdayparty import BirthdayParty
+
+
+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 not party:
+ print(component.errors())
+ del engine
+ sys.exit(-1)
+host = party.host
+print(f"{host.name} is having a birthday!")
+if isinstance(host, Boy):
+ print("He is inviting:")
+else:
+ print("She is inviting:")
+for g in range(party.guestCount()):
+ name = party.guest(g).name
+ print(f" {name}")
+del engine
+sys.exit(0)
diff --git a/examples/declarative/referenceexamples/coercion/person.py b/examples/declarative/referenceexamples/coercion/person.py
new file mode 100644
index 000000000..3bb7e7248
--- /dev/null
+++ b/examples/declarative/referenceexamples/coercion/person.py
@@ -0,0 +1,84 @@
+#############################################################################
+##
+## 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, QmlUncreatable
+
+# To be used on the @QmlElement decorator
+# (QML_IMPORT_MINOR_VERSION is optional)
+QML_IMPORT_NAME = "examples.coercion.people"
+QML_IMPORT_MAJOR_VERSION = 1
+
+
+@QmlElement
+@QmlUncreatable("Person is an abstract base class.")
+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
+
+
+@QmlElement
+class Boy(Person):
+ def __init__(self, parent=None):
+ super().__init__(parent)
+
+
+@QmlElement
+class Girl(Person):
+ def __init__(self, parent=None):
+ super().__init__(parent)
diff --git a/examples/declarative/referenceexamples/properties/doc/properties.rst b/examples/declarative/referenceexamples/properties/doc/properties.rst
index a4678b89f..909434c3c 100644
--- a/examples/declarative/referenceexamples/properties/doc/properties.rst
+++ b/examples/declarative/referenceexamples/properties/doc/properties.rst
@@ -1,4 +1,4 @@
-_qml-object-and-list-property-types-example:
+.. _qml-object-and-list-property-types-example:
Extending QML - Object and List Property Types Example
======================================================