diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-05-02 16:56:50 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-05-03 14:42:15 +0200 |
| commit | 2388ac63d3784e59b416f993e123241cfb7f2ffa (patch) | |
| tree | ef367674f06055ed1c63261a119ecc246f9c7fd7 /examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion | |
| parent | 42be729770eeb5d40802cfc697bddd744ba2623f (diff) | |
QML reference examples: Add notify signals and final attribute to properties
Task-number: PYSIDE-2206
Task-number: QTBUG-111033
Pick-to: 6.5
Change-Id: I0541a3bbb4e5696962802da7f91ab79682700124
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion')
2 files changed, 19 insertions, 9 deletions
diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.py b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.py index 47dddc85d..764815175 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.py +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.py @@ -1,7 +1,7 @@ -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -from PySide6.QtCore import QObject, Property +from PySide6.QtCore import QObject, Property, Signal from PySide6.QtQml import QmlElement, ListProperty from person import Person @@ -15,19 +15,23 @@ QML_IMPORT_MAJOR_VERSION = 1 @QmlElement class BirthdayParty(QObject): + host_changed = Signal() + guests_changed = Signal() def __init__(self, parent=None): super().__init__(parent) self._host = None self._guests = [] - @Property(Person) + @Property(Person, notify=host_changed, final=True) def host(self): return self._host @host.setter def host(self, h): - self._host = h + if self._host != h: + self._host = h + self.host_changed.emit() def guest(self, n): return self._guests[n] @@ -37,5 +41,6 @@ class BirthdayParty(QObject): def appendGuest(self, guest): self._guests.append(guest) + self.guests_changed.emit() - guests = ListProperty(Person, appendGuest) + guests = ListProperty(Person, appendGuest, notify=guests_changed, final=True) diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/person.py b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/person.py index 189f2573e..57e73e6f5 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/person.py +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/person.py @@ -1,7 +1,7 @@ # 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.QtCore import QObject, Property, Signal from PySide6.QtQml import QmlElement, QmlUncreatable # To be used on the @QmlElement decorator @@ -13,20 +13,25 @@ QML_IMPORT_MAJOR_VERSION = 1 @QmlElement @QmlUncreatable("Person is an abstract base class.") class Person(QObject): + name_changed = Signal() + shoe_size_changed = Signal() + def __init__(self, parent=None): super().__init__(parent) self._name = '' self._shoe_size = 0 - @Property(str) + @Property(str, notify=name_changed, final=True) def name(self): return self._name @name.setter def name(self, n): - self._name = n + if self._name != n: + self._name = n + self.name_changed.emit() - @Property(int) + @Property(int, notify=shoe_size_changed, final=True) def shoe_size(self): return self._shoe_size |
