aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qml/tutorials/extending-qml/chapter4-customPropertyTypes/doc/chapter4-customPropertyTypes.rst
blob: f7c3efb1110ee15bb0ff712ee941102eed15c873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
.. _qml-chapter4-custompropertytypes:

Extending QML - Using Custom Property Types
===========================================

This is the fourth of a series of 6 examples forming a tutorial about extending
QML with Python.

The ``PieChart`` type currently has a string-type property and a color-type property.
It could have many other types of properties. For example, it could have an
int-type property to store an identifier for each chart:

.. code-block:: python

    class PieChart(QQuickPaintedItem):
        chartIdChanged = Signal()

        @Property(int, notify=chartIdChanged)
        def chartId(self):
            pass

        @chartId.setter
        def setChartId(self, chartId):
            pass

.. code-block:: javascript

    // QML
    PieChart {
        ...
        chartId: 100
    }

Aside from ``int``, we could use various other property types. Many of the Qt
data types such as ``QColor``, ``QSize`` and ``QRect`` are automatically
supported from QML.

If we want to create a property whose type is not supported by QML by default,
we need to register the type with the QML engine.

For example, let's replace the use of the ``property`` with a type called
``PieSlice`` that has a ``color`` property. Instead of assigning a color,
we assign an ``PieSlice`` value which itself contains a ``color``:

.. literalinclude:: app.qml
    :lineno-start: 4
    :lines: 4-22

Like ``PieChart``, this new ``PieSlice`` type inherits from
``QQuickPaintedItem``, is exposed via the ``QmlElement`` decorator and declares
its properties with the ``Property`` decorator:

.. literalinclude:: customPropertyTypes.py
    :lineno-start: 21
    :lines: 21-40

To use it in ``PieChart``, we modify the ``color`` property declaration
and associated method signatures:

.. literalinclude:: customPropertyTypes.py
    :lineno-start: 58
    :lines: 58-65

There is one thing to be aware of when implementing ``setPieSlice()``. The
``PieSlice`` is a visual item, so it must be set as a child of the ``PieChart``
using ``QQuickItem.setParentItem()`` so that the ``PieChart`` knows to paint
this child item when its contents are drawn.

As with ``PieChart``, we add the ``Charts`` type namespace, version 1.0:

.. literalinclude:: customPropertyTypes.py
    :lineno-start: 15
    :lines: 15-18