aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/doc/advanced5-Attached-properties.rst
blob: 14b4bddb0ffd05c84d455d40a9ef7f983ceebcf5 (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
.. _qml-advanced-advanced5-attached-properties:

Extending QML (advanced) - Attached Properties
==============================================

This is the fifth of a series of 6 examples forming a tutorial using the
example of a birthday party to demonstrate some of the advanced features of
QML.

The time has come for the host to send out invitations. To keep track of which
guests have responded to the invitation and when, we need somewhere to store
that information. Storing it in the ``BirthdayParty`` object iself would not
really fit. A better way would be to store the responses as attached objects to
the party object.

First, we declare the ``BirthdayPartyAttached`` class which holds the guest reponses.

.. literalinclude:: birthdayparty.py
    :lineno-start: 16
    :lines: 16-32

And we attach it to the ``BirthdayParty`` class and define
``qmlAttachedProperties()`` to return the attached object.

.. literalinclude:: birthdayparty.py
    :lineno-start: 34
    :lines: 34-38

.. literalinclude:: birthdayparty.py
    :lineno-start: 67
    :lines: 67-69

Now, attached objects can be used in the QML to hold the rsvp information of
the invited guests.

.. literalinclude:: People/Main.qml
    :lineno-start: 6
    :lines: 6-22

Finally, the information can be accessed in the following way.

.. literalinclude:: main.py
    :lineno-start: 36
    :lines: 36-39

The program outputs the following summary of the party to come::

    "Jack Smith" is having a birthday!
    He is inviting:
        "Robert Campbell" RSVP date: "Wed Mar 1 2023"
        "Leo Hodges" RSVP date: "Mon Mar 6 2023"