diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-06 15:15:56 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-07 15:49:13 +0000 |
| commit | e904491d87e9b91458d91d3d4e6f5d7c95127028 (patch) | |
| tree | b5bf66f9c809156c103c3f62b70b7311d2ad6e3e /examples/declarative/referenceexamples/adding/doc | |
| parent | 1db8fe5d80496be390e19d31ca9c8b87ed7579a4 (diff) | |
Add the 'adding' example of the QML reference examples
Task-number: PYSIDE-841
Pick-to: 6.2
Change-Id: I274039d0642a84f526008c8ecc27f2727c84a3cd
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples/declarative/referenceexamples/adding/doc')
| -rw-r--r-- | examples/declarative/referenceexamples/adding/doc/adding.rst | 65 |
1 files changed, 65 insertions, 0 deletions
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. |
