aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-11-05 11:33:00 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-11-10 22:43:40 +0100
commit5b8d4517fabda5713a4023b9692ff5fe77162237 (patch)
tree5d2cefeab80378db9798e86194548f95b748475a /examples
parent20fee1eb56f64ecc6d356abe658da14d09e7d09a (diff)
Add the 'methods' example of the QML reference examples
Task-number: PYSIDE-841 Pick-to: 6.2 Change-Id: I99a592e6a9c347c46dd9bb86a9e925d0f70c34c6 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/declarative/referenceexamples/methods/birthdayparty.py84
-rw-r--r--examples/declarative/referenceexamples/methods/doc/methods.rst15
-rw-r--r--examples/declarative/referenceexamples/methods/example.qml66
-rw-r--r--examples/declarative/referenceexamples/methods/main.py69
-rw-r--r--examples/declarative/referenceexamples/methods/methods.pyproject3
-rw-r--r--examples/declarative/referenceexamples/methods/person.py71
6 files changed, 308 insertions, 0 deletions
diff --git a/examples/declarative/referenceexamples/methods/birthdayparty.py b/examples/declarative/referenceexamples/methods/birthdayparty.py
new file mode 100644
index 000000000..ca60da771
--- /dev/null
+++ b/examples/declarative/referenceexamples/methods/birthdayparty.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, Slot
+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.methods.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)
+
+ @Slot(str)
+ def invite(self, name):
+ guest = Person(self)
+ guest.name = name
+ self.appendGuest(guest)
+
+ guests = ListProperty(Person, appendGuest)
diff --git a/examples/declarative/referenceexamples/methods/doc/methods.rst b/examples/declarative/referenceexamples/methods/doc/methods.rst
new file mode 100644
index 000000000..bda2ede5a
--- /dev/null
+++ b/examples/declarative/referenceexamples/methods/doc/methods.rst
@@ -0,0 +1,15 @@
+.. _qml-methods-example:
+
+Extending QML - Methods Example
+===============================
+
+This example builds on the :ref:`qml-adding-types-example`,
+the :ref:`qml-object-and-list-property-types-example` and
+the :ref:`qml-inheritance-and-coercion-example`.
+
+The Methods Example has an additional method in the ``BirthdayParty`` class:
+``invite()``. ``invite()`` is decorated with ``@Slot`` so that it can be
+called from QML.
+
+In ``example.qml``, the ``invite()`` method is called
+in the ``QtQml.Component.completed()`` signal handler.
diff --git a/examples/declarative/referenceexamples/methods/example.qml b/examples/declarative/referenceexamples/methods/example.qml
new file mode 100644
index 000000000..bbbc154ca
--- /dev/null
+++ b/examples/declarative/referenceexamples/methods/example.qml
@@ -0,0 +1,66 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 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 QtQuick
+import examples.methods.people
+
+BirthdayParty {
+ host: Person {
+ name: "Bob Jones"
+ shoe_size: 12
+ }
+ guests: [
+ Person { name: "Leo Hodges" },
+ Person { name: "Jack Smith" },
+ Person { name: "Anne Brown" }
+ ]
+
+ Component.onCompleted: invite("William Green")
+}
diff --git a/examples/declarative/referenceexamples/methods/main.py b/examples/declarative/referenceexamples/methods/main.py
new file mode 100644
index 000000000..9dd9086ab
--- /dev/null
+++ b/examples/declarative/referenceexamples/methods/main.py
@@ -0,0 +1,69 @@
+#############################################################################
+##
+## 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/methods 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
+
+
+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!\nThey are 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/methods/methods.pyproject b/examples/declarative/referenceexamples/methods/methods.pyproject
new file mode 100644
index 000000000..3c01c40c2
--- /dev/null
+++ b/examples/declarative/referenceexamples/methods/methods.pyproject
@@ -0,0 +1,3 @@
+{
+ "files": ["main.py", "birthdayparty.py", "person.py", "example.qml"]
+}
diff --git a/examples/declarative/referenceexamples/methods/person.py b/examples/declarative/referenceexamples/methods/person.py
new file mode 100644
index 000000000..43cdd09e3
--- /dev/null
+++ b/examples/declarative/referenceexamples/methods/person.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$
+##
+#############################################################################
+
+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.methods.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