aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qml/tutorials/extending-qml-advanced/adding
diff options
context:
space:
mode:
Diffstat (limited to 'examples/qml/tutorials/extending-qml-advanced/adding')
-rw-r--r--examples/qml/tutorials/extending-qml-advanced/adding/People/Main.qml9
-rw-r--r--examples/qml/tutorials/extending-qml-advanced/adding/People/qmldir3
-rw-r--r--examples/qml/tutorials/extending-qml-advanced/adding/adding.pyproject4
-rw-r--r--examples/qml/tutorials/extending-qml-advanced/adding/doc/adding.rst67
-rw-r--r--examples/qml/tutorials/extending-qml-advanced/adding/main.py30
-rw-r--r--examples/qml/tutorials/extending-qml-advanced/adding/person.py34
6 files changed, 147 insertions, 0 deletions
diff --git a/examples/qml/tutorials/extending-qml-advanced/adding/People/Main.qml b/examples/qml/tutorials/extending-qml-advanced/adding/People/Main.qml
new file mode 100644
index 000000000..8d963a861
--- /dev/null
+++ b/examples/qml/tutorials/extending-qml-advanced/adding/People/Main.qml
@@ -0,0 +1,9 @@
+// Copyright (C) 2017 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+import People
+
+Person {
+ name: "Bob Jones"
+ shoe_size: 12
+}
diff --git a/examples/qml/tutorials/extending-qml-advanced/adding/People/qmldir b/examples/qml/tutorials/extending-qml-advanced/adding/People/qmldir
new file mode 100644
index 000000000..a2bd9515a
--- /dev/null
+++ b/examples/qml/tutorials/extending-qml-advanced/adding/People/qmldir
@@ -0,0 +1,3 @@
+module People
+typeinfo coercion.qmltypes
+Main 1.0 Main.qml
diff --git a/examples/qml/tutorials/extending-qml-advanced/adding/adding.pyproject b/examples/qml/tutorials/extending-qml-advanced/adding/adding.pyproject
new file mode 100644
index 000000000..3219f79ca
--- /dev/null
+++ b/examples/qml/tutorials/extending-qml-advanced/adding/adding.pyproject
@@ -0,0 +1,4 @@
+{
+ "files": ["main.py", "person.py",
+ "People/Main.qml", "People/qmldir"]
+}
diff --git a/examples/qml/tutorials/extending-qml-advanced/adding/doc/adding.rst b/examples/qml/tutorials/extending-qml-advanced/adding/doc/adding.rst
new file mode 100644
index 000000000..4c1b3bdae
--- /dev/null
+++ b/examples/qml/tutorials/extending-qml-advanced/adding/doc/adding.rst
@@ -0,0 +1,67 @@
+.. _qml-adding-types-example:
+
+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 = "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/qml/tutorials/extending-qml-advanced/adding/main.py b/examples/qml/tutorials/extending-qml-advanced/adding/main.py
new file mode 100644
index 000000000..ec703dbf3
--- /dev/null
+++ b/examples/qml/tutorials/extending-qml-advanced/adding/main.py
@@ -0,0 +1,30 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+"""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
+from PySide6.QtQml import QQmlComponent, QQmlEngine
+
+from person import Person # noqa: F401
+
+
+if __name__ == '__main__':
+ app = QCoreApplication(sys.argv)
+
+ engine = QQmlEngine()
+ engine.addImportPath(Path(__file__).parent)
+ component = QQmlComponent(engine)
+ component.loadFromModule("People", "Main")
+
+ 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/qml/tutorials/extending-qml-advanced/adding/person.py b/examples/qml/tutorials/extending-qml-advanced/adding/person.py
new file mode 100644
index 000000000..526eae714
--- /dev/null
+++ b/examples/qml/tutorials/extending-qml-advanced/adding/person.py
@@ -0,0 +1,34 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+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 = "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