diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-05-02 10:41:54 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-05-10 14:03:05 +0000 |
commit | 5fed0abe1c06b21358c5cd5a8ff8fdd10a073620 (patch) | |
tree | b50b84c0bc32c1d9470e079b911ee00e38654c60 /examples | |
parent | 0513274b549a66484bb0b554d4f143621c9da7be (diff) |
QML reference examples: Add the add advanced1-Base-project
Task-number: PYSIDE-2206
Task-number: QTBUG-111033
Change-Id: I8b162f2f5cb6e7099c74e751bcdf95dea5d7d118
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit 58c304ca83d5fc66707cddc0998b2b63b1148508)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples')
7 files changed, 199 insertions, 0 deletions
diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/People/Main.qml b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/People/Main.qml new file mode 100644 index 000000000..c14051371 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/People/Main.qml @@ -0,0 +1,16 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import People + +BirthdayParty { + host: Person { + name: "Bob Jones" + shoe_size: 12 + } + guests: [ + Person { name: "Leo Hodges" }, + Person { name: "Jack Smith" }, + Person { name: "Anne Brown" } + ] +} diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/People/qmldir b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/People/qmldir new file mode 100644 index 000000000..a2bd9515a --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/People/qmldir @@ -0,0 +1,3 @@ +module People +typeinfo coercion.qmltypes +Main 1.0 Main.qml diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/advanced1-Base-project.pyproject b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/advanced1-Base-project.pyproject new file mode 100644 index 000000000..09942ebcc --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/advanced1-Base-project.pyproject @@ -0,0 +1,4 @@ +{ + "files": ["main.py", "birthdayparty.py", "person.py", + "People/Main.qml", "People/qmldir"] +} diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/birthdayparty.py b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/birthdayparty.py new file mode 100644 index 000000000..764815175 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/birthdayparty.py @@ -0,0 +1,46 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +from PySide6.QtCore import QObject, Property, Signal +from PySide6.QtQml import QmlElement, ListProperty + +from person import Person + + +# To be used on the @QmlElement decorator +# (QML_IMPORT_MINOR_VERSION is optional) +QML_IMPORT_NAME = "People" +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, notify=host_changed, final=True) + def host(self): + return self._host + + @host.setter + def host(self, h): + if self._host != h: + self._host = h + self.host_changed.emit() + + def guest(self, n): + return self._guests[n] + + def guestCount(self): + return len(self._guests) + + def appendGuest(self, guest): + self._guests.append(guest) + self.guests_changed.emit() + + guests = ListProperty(Person, appendGuest, notify=guests_changed, final=True) diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/doc/advanced1-Base-project.rst b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/doc/advanced1-Base-project.rst new file mode 100644 index 000000000..90a73b78d --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/doc/advanced1-Base-project.rst @@ -0,0 +1,57 @@ +.. _qml-advanced1-base-project: + +Extending QML (advanced) - BirthdayParty Base Project +===================================================== + +This is the first 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 code for the various features explained below is based on this +birthday party project and relies on some of the material in the basic +tutorial. This simple example is then expanded upon to illustrate the various +QML extensions explained below. The complete code for each new extension to the +code can be found at the end of the respective page. + +The base project defines the ``Person`` class and the ``BirthdayParty`` class, +which model the attendees and the party itself respectively. + +.. literalinclude:: person.py + :lineno-start: 13 + :lines: 13-41 + +.. literalinclude:: birthdayparty.py + :lineno-start: 16 + :lines: 16-46 + +All the information about the party can then be stored in the corresponding QML +file. + +.. literalinclude:: People/Main.qml + :lineno-start: 4 + :lines: 4-16 + + +The ``main.py`` file creates a simple shell application that displays whose +birthday it is and who is invited to their party. + +.. literalinclude:: main.py + :lineno-start: 17 + :lines: 17-21 + +The app outputs the following summary of the party:: + + "Bob Jones" is having a birthday! + They are inviting: + "Leo Hodges" + "Jack Smith" + "Anne Brown" + +Outlook +------- + +The following sections go into how to add support for ``Boy`` and ``Girl`` +attendees instead of just ``Person`` by using inheritance and coercion, how to +make use of default properties to implicitly assign attendees of the party as +guests, how to assign properties as groups instead of one by one, how to use +attached objects to keep track of invited guests' reponses, how to use a +property value source to display the lyrics of the happy birthday song over +time, and how to expose third party objects to QML. diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/main.py b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/main.py new file mode 100644 index 000000000..dc07a2a5f --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/main.py @@ -0,0 +1,32 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +"""PySide6 port of the qml/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project example from Qt v6.x""" + +from pathlib import Path +import sys + +from PySide6.QtCore import QCoreApplication +from PySide6.QtQml import QQmlComponent, QQmlEngine + +from person import Person +from birthdayparty import BirthdayParty + + +app = QCoreApplication(sys.argv) +engine = QQmlEngine() +engine.addImportPath(Path(__file__).parent) +component = QQmlComponent(engine) +component.loadFromModule("People", "Main") +party = component.create() +if not party: + print(component.errors()) + del engine + sys.exit(-1) +host = party.host +print(f"{host.name} is having a birthday!\nThey are inviting:") +for g in range(party.guestCount()): + name = party.guest(g).name + print(f" {name}") +del engine +sys.exit(0) diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/person.py b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/person.py new file mode 100644 index 000000000..60dc9d882 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/person.py @@ -0,0 +1,41 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +from PySide6.QtCore import QObject, Property, Signal +from PySide6.QtQml import QmlElement + +# To be used on the @QmlElement decorator +# (QML_IMPORT_MINOR_VERSION is optional) +QML_IMPORT_NAME = "People" +QML_IMPORT_MAJOR_VERSION = 1 + + +@QmlElement +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, notify=name_changed, final=True) + def name(self): + return self._name + + @name.setter + def name(self, n): + if self._name != n: + self._name = n + self.name_changed.emit() + + @Property(int, notify=shoe_size_changed, final=True) + def shoe_size(self): + return self._shoe_size + + @shoe_size.setter + def shoe_size(self, s): + if self._shoe_size != s: + self._shoe_size = s + self.shoe_size_changed.emit() |