diff options
author | Olivier De Cannière <olivier.decanniere@qt.io> | 2023-03-17 15:42:47 +0100 |
---|---|---|
committer | Olivier De Cannière <olivier.decanniere@qt.io> | 2023-03-30 18:02:16 +0200 |
commit | 405bd4299819e39397cea0090a9442fd4b6ce911 (patch) | |
tree | c1254e18a8ab47cc6bde048eb52ad0d89c2492c8 /examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/birthdayparty.cpp | |
parent | 03ff348b4942ae29dd5bc2bd563998d7ba82ecd7 (diff) | |
download | qtdeclarative-405bd4299819e39397cea0090a9442fd4b6ce911.tar.gz |
Doc: Revamp "Extending QML" examples into a tutorial
The examples in the "Extending QML" series were often redundant with the
information of the "Writing QML Extensions with C++" tutorial, had
outdated code and sometimes had no documentation. The examples that
covered topics not mentioned in the first tutorial were revamped into a
second "advanced" tutorial extending the first one. The others were
removed. The remaining examples were largely based on the same example
code of a birthday party. This code was slightly adapted and separated
into 7 states, each building upon the previous, with the code change
illustrating the associated feature. A tutorial page, in the style of
the first one, was added documenting the different QML features and
the required code changes in the example project.
Links in the documentation from and to the affected pages were update
as best as possible.
Pick-to: 6.5
Fixes: QTBUG-111033
Change-Id: I9d97e8b32b128c1624d67525996fa14d493909d3
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/birthdayparty.cpp')
-rw-r--r-- | examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/birthdayparty.cpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/birthdayparty.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/birthdayparty.cpp new file mode 100644 index 0000000000..0379a7accf --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/birthdayparty.cpp @@ -0,0 +1,117 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "birthdayparty.h" + +QDate BirthdayPartyAttached::rsvp() const +{ + return m_rsvp; +} + +void BirthdayPartyAttached::setRsvp(QDate rsvpDate) +{ + if (m_rsvp != rsvpDate) { + m_rsvp = rsvpDate; + emit rsvpChanged(); + } +} + +Person *BirthdayParty::host() const +{ + return m_host; +} + +void BirthdayParty::setHost(Person *host) +{ + if (m_host != host) { + m_host = host; + emit hostChanged(); + } +} + +QQmlListProperty<Person> BirthdayParty::guests() +{ + return { this, + this, + &BirthdayParty::appendGuest, + &BirthdayParty::guestCount, + &BirthdayParty::guest, + &BirthdayParty::clearGuests, + &BirthdayParty::replaceGuest, + &BirthdayParty::removeLastGuest }; +} + +void BirthdayParty::appendGuest(Person *guest) +{ + m_guests.append(guest); + emit guestsChanged(); +} + +qsizetype BirthdayParty::guestCount() const +{ + return m_guests.count(); +} + +Person *BirthdayParty::guest(qsizetype index) const +{ + return m_guests.at(index); +} + +void BirthdayParty::clearGuests() +{ + if (!m_guests.empty()) { + m_guests.clear(); + emit guestsChanged(); + } +} + +void BirthdayParty::replaceGuest(qsizetype index, Person *guest) +{ + if (m_guests.size() > index) { + m_guests[index] = guest; + emit guestsChanged(); + } +} + +void BirthdayParty::removeLastGuest() +{ + if (!m_guests.empty()) { + m_guests.removeLast(); + emit guestsChanged(); + } +} + +void BirthdayParty::appendGuest(QQmlListProperty<Person> *list, Person *guest) +{ + static_cast<BirthdayParty *>(list->data)->appendGuest(guest); +} + +void BirthdayParty::clearGuests(QQmlListProperty<Person> *list) +{ + static_cast<BirthdayParty *>(list->data)->clearGuests(); +} + +void BirthdayParty::replaceGuest(QQmlListProperty<Person> *list, qsizetype index, Person *guest) +{ + static_cast<BirthdayParty *>(list->data)->replaceGuest(index, guest); +} + +void BirthdayParty::removeLastGuest(QQmlListProperty<Person> *list) +{ + static_cast<BirthdayParty *>(list->data)->removeLastGuest(); +} + +Person *BirthdayParty::guest(QQmlListProperty<Person> *list, qsizetype index) +{ + return static_cast<BirthdayParty *>(list->data)->guest(index); +} + +qsizetype BirthdayParty::guestCount(QQmlListProperty<Person> *list) +{ + return static_cast<BirthdayParty *>(list->data)->guestCount(); +} + +BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) +{ + return new BirthdayPartyAttached(object); +} |