From d9ad45f67fd8f64b347ec3d516f70d007fff1d34 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 6 Oct 2021 15:15:56 +0200 Subject: Add the 'adding' example of the QML reference examples Task-number: PYSIDE-841 Change-Id: I274039d0642a84f526008c8ecc27f2727c84a3cd Reviewed-by: Christian Tismer (cherry picked from commit e904491d87e9b91458d91d3d4e6f5d7c95127028) Reviewed-by: Qt Cherry-pick Bot --- .../referenceexamples/adding/adding.pyproject | 5 ++ .../referenceexamples/adding/doc/adding.rst | 65 +++++++++++++++++++ .../referenceexamples/adding/example.qml | 56 +++++++++++++++++ .../declarative/referenceexamples/adding/main.py | 67 ++++++++++++++++++++ .../declarative/referenceexamples/adding/person.py | 72 ++++++++++++++++++++++ 5 files changed, 265 insertions(+) create mode 100644 examples/declarative/referenceexamples/adding/adding.pyproject create mode 100644 examples/declarative/referenceexamples/adding/doc/adding.rst create mode 100644 examples/declarative/referenceexamples/adding/example.qml create mode 100644 examples/declarative/referenceexamples/adding/main.py create mode 100644 examples/declarative/referenceexamples/adding/person.py diff --git a/examples/declarative/referenceexamples/adding/adding.pyproject b/examples/declarative/referenceexamples/adding/adding.pyproject new file mode 100644 index 000000000..46df4b253 --- /dev/null +++ b/examples/declarative/referenceexamples/adding/adding.pyproject @@ -0,0 +1,5 @@ +{ + "files": ["example.qml", + "main.py", + "person.py"] +} diff --git a/examples/declarative/referenceexamples/adding/doc/adding.rst b/examples/declarative/referenceexamples/adding/doc/adding.rst new file mode 100644 index 000000000..b060f3c2c --- /dev/null +++ b/examples/declarative/referenceexamples/adding/doc/adding.rst @@ -0,0 +1,65 @@ +Extending QML - Adding Types Example +==================================== + +The Adding Types Example shows how to add a new object type, ``Person``, to QML. +The ``Person`` type can be used from QML like this: + +.. code-block:: javascript + + import examples.adding.people + + Person { + name: "Bob Jones" + shoe_size: 12 + } + +Declare the Person Class +------------------------ + +All QML types map to C++ types. Here we declare a basic C++ Person class +with the two properties we want accessible on the QML type - name and shoeSize. +Although in this example we use the same name for the C++ class as the QML +type, the C++ class can be named differently, or appear in a namespace. + +The Person class implementation is quite basic. The property accessors simply +return members of the object instance. + +.. code-block:: python + + 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.adding.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 + +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/adding/example.qml b/examples/declarative/referenceexamples/adding/example.qml new file mode 100644 index 000000000..e452b2283 --- /dev/null +++ b/examples/declarative/referenceexamples/adding/example.qml @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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.adding.people + +Person { + name: "Bob Jones" + shoe_size: 12 +} diff --git a/examples/declarative/referenceexamples/adding/main.py b/examples/declarative/referenceexamples/adding/main.py new file mode 100644 index 000000000..ffa10e9d3 --- /dev/null +++ b/examples/declarative/referenceexamples/adding/main.py @@ -0,0 +1,67 @@ +############################################################################# +## +## 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/adding 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 + + +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) + + person = component.create() + if person: + print(f"The person's name is {person.name}") + print(f"They wear a {person.shoe_size} sized shoe") + else: + print(component.errors()) + del engine + sys.exit(0) diff --git a/examples/declarative/referenceexamples/adding/person.py b/examples/declarative/referenceexamples/adding/person.py new file mode 100644 index 000000000..23ac5378d --- /dev/null +++ b/examples/declarative/referenceexamples/adding/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.adding.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 + -- cgit v1.2.3