diff options
Diffstat (limited to 'examples/qml')
147 files changed, 2157 insertions, 2659 deletions
diff --git a/examples/qml/CMakeLists.txt b/examples/qml/CMakeLists.txt index 6b3e51bf9f..3d2cbc4503 100644 --- a/examples/qml/CMakeLists.txt +++ b/examples/qml/CMakeLists.txt @@ -1,7 +1,6 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -add_subdirectory(referenceexamples) add_subdirectory(tutorials) if(TARGET Qt::Quick) qt_internal_add_example(qmlextensionplugins) diff --git a/examples/qml/doc/src/qml-extending.qdoc b/examples/qml/doc/src/qml-extending.qdoc deleted file mode 100644 index 5d92f0a51c..0000000000 --- a/examples/qml/doc/src/qml-extending.qdoc +++ /dev/null @@ -1,339 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only - -/*! -\example referenceexamples/adding -\title Extending QML - Adding Types Example -\brief Exporting C++ Classes. -\ingroup qmlextendingexamples - -The Adding Types Example shows how to add a new object type, \c Person, to QML. -The \c Person type can be used from QML like this: - -\snippet referenceexamples/adding/example.qml 0 - -\section1 Declare the Person Class - -All QML types map to C++ types. Here we declare a basic C++ Person class -with the two properties we want accessible on the QML type - name and shoeSize. -Although in this example we use the same name for the C++ class as the QML -type, the C++ class can be named differently, or appear in a namespace. - -\snippet referenceexamples/adding/person.h 0 - -\section1 Define the Person Class - -\snippet referenceexamples/adding/person.cpp 0 - -The Person class implementation is quite basic. The property accessors simply -return members of the object instance. - -\section1 Running the Example - -The main.cpp file in the example includes a simple shell application that -loads and runs the QML snippet shown at the beginning of this page. -*/ - -/*! -\example referenceexamples/extended -\title Extending QML - Extension Objects Example -\brief Extension Objects. -\ingroup qmlextendingexamples - -This example builds on: -\list -\li \l {Extending QML - Adding Types Example} -\endlist - -Shows how to use \l {QML_EXTENDED} to provide an -\l {Registering Extension Objects}{extension object} to a \l QLineEdit without modifying or -subclassing it. - -Firstly, the LineEditExtension class is registered with the QML system as an -extension of QLineEdit. We declare a foreign type to do this as we cannot modify -Qt's internal QLineEdit class. - -\snippet referenceexamples/extended/lineedit.h 0 - -Note the usage of \l QML_NAMED_ELEMENT() instead of \l QML_ELEMENT. -QML_ELEMENT uses the name of the containing type by default, "LineEditExtension" in this case. -As the class being an extension class is an implementation detail, we choose the more natural name "LineEdit" instead - -The QML engine then instantiates a \l QLineEdit: - -\snippet referenceexamples/extended/main.cpp 1 - -In QML, a property is set on the line edit that only exists in the LineEditExtension class: - -\snippet referenceexamples/extended/example.qml 0 - -The extension type performs calls on the \l QLineEdit that otherwise will -not be accessible to the QML engine. -*/ - -/*! -\example referenceexamples/properties -\title Extending QML - Object and List Property Types Example -\brief Exporting C++ Properties. -\ingroup qmlextendingexamples - -This example builds on: -\list -\li \l {Extending QML - Adding Types Example} -\endlist - -The Object and List Property Types example shows how to add object and list -properties in QML. This example adds a BirthdayParty type that specifies -a birthday party, consisting of a celebrant and a list of guests. People are -specified using the People QML type built in the previous example. - -\snippet referenceexamples/properties/example.qml 0 - -\section1 Declare the BirthdayParty - -The BirthdayParty class is declared like this: - -\snippet referenceexamples/properties/birthdayparty.h 0 -\snippet referenceexamples/properties/birthdayparty.h 1 -\snippet referenceexamples/properties/birthdayparty.h 2 -\snippet referenceexamples/properties/birthdayparty.h 3 - -The class contains a member to store the celebrant object, and also a -QList<Person *> member. - -In QML, the type of a list properties - and the guests property is a list of -people - are all of type QQmlListProperty<T>. QQmlListProperty is simple value -type that contains a set of function pointers. QML calls these function -pointers whenever it needs to read from, write to or otherwise interact with -the list. In addition to concrete lists like the people list used in this -example, the use of QQmlListProperty allows for "virtual lists" and other advanced -scenarios. - -\section2 Define the BirthdayParty - -The implementation of BirthdayParty property accessors is straight forward. - -\snippet referenceexamples/properties/birthdayparty.cpp 0 - -\section1 Running the Example - -The main.cpp file in the example includes a simple shell application that -loads and runs the QML snippet shown at the beginning of this page. -*/ - -/*! -\example referenceexamples/coercion -\title Extending QML - Inheritance and Coercion Example -\brief C++ Inheritance and Coercion. -\ingroup qmlextendingexamples - -This example builds on: -\list -\li \l {Extending QML - Object and List Property Types Example} -\li \l {Extending QML - Adding Types Example} -\endlist - -The Inheritance and Coercion Example shows how to use base classes to assign -types of more than one type to a property. It specializes the Person type -developed in the previous examples into two types - a \c Boy and a \c Girl. - -\snippet referenceexamples/coercion/example.qml 0 - -\section1 Declare Boy and Girl - -\snippet referenceexamples/coercion/person.h 1 - -The Person class remains unaltered in this example and the Boy and Girl C++ -classes are trivial extensions of it. The types and their QML name are -registered with the QML engine. - -As an example, the inheritance used here is a little contrived, but in real -applications it is likely that the two extensions would add additional -properties or modify the Person classes behavior. - -\section2 Define People as a Base Class - -The implementation of the People class itself has not changed since the -previous example. However, as we have repurposed the People class as a common -base for Boy and Girl, we want to prevent it from being instantiated from QML -directly - an explicit Boy or Girl should be instantiated instead. - -\snippet referenceexamples/coercion/person.h 0 - -While we want to disallow instantiating Person from within QML, it still needs -to be registered with the QML engine, so that it can be used as a property type -and other types can be coerced to it. This is what the QML_UNCREATABLE macro -does. - -\section1 Running the Example - -The BirthdayParty type has not changed since the previous example. The -celebrant and guests property still use the People type. - -\snippet referenceexamples/coercion/birthdayparty.h 0 - -However, as all three types, Person, Boy and Girl, have been registered with the -QML system, on assignment QML automatically (and type-safely) converts the Boy -and Girl objects into a Person. - -The main.cpp file in the example includes a simple shell application that -loads and runs the QML snippet shown at the beginning of this page. -*/ - -/*! -\example referenceexamples/default -\title Extending QML - Default Property Example -\brief Default Property. -\ingroup qmlextendingexamples - -This example builds on: -\list -\li \l {Extending QML - Inheritance and Coercion Example} -\li \l {Extending QML - Object and List Property Types Example} -\li \l {Extending QML - Adding Types Example} -\endlist - -The Default Property Example is a minor modification of the -\l {Extending QML - Inheritance and Coercion Example} that simplifies the -specification of a BirthdayParty through the use of a default property. - -\snippet referenceexamples/default/example.qml 0 - -\section1 Declaring the BirthdayParty Class - -The only difference between this example and the last, is the addition of the -\c DefaultProperty class info annotation. - -\snippet referenceexamples/default/birthdayparty.h 0 - -The default property specifies the property to assign to whenever an explicit -property is not specified, in the case of the BirthdayParty type the guest -property. It is purely a syntactic simplification, the behavior is identical -to specifying the property by name, but it can add a more natural feel in many -situations. The default property must be either an object or list property. - -\section1 Running the Example - -The main.cpp file in the example includes a simple shell application that -loads and runs the QML snippet shown at the beginning of this page. -*/ - -/*! -\example referenceexamples/grouped -\title Extending QML - Grouped Properties Example -\brief Grouped Properties. -\ingroup qmlextendingexamples - -This example builds on: -\list -\li \l {Extending QML - Default Property Example} -\li \l {Extending QML - Inheritance and Coercion Example} -\li \l {Extending QML - Object and List Property Types Example} -\li \l {Extending QML - Adding Types Example} -\endlist - -*/ - -/*! -\example referenceexamples/attached -\title Extending QML - Attached Properties Example -\brief Attached Properties. -\ingroup qmlextendingexamples - -This example demonstrates how to create custom -\l {Attached Properties and Attached Signal Handlers} {attached properties}. -For a more in-depth description on how one can create attached properties, -see \l {Providing Attached Properties}. - - -This example builds on: -\list -\li \l {Extending QML - Grouped Properties Example} -\li \l {Extending QML - Default Property Example} -\li \l {Extending QML - Inheritance and Coercion Example} -\li \l {Extending QML - Object and List Property Types Example} -\li \l {Extending QML - Adding Types Example} -\endlist - -*/ - -/*! -\example referenceexamples/signal -\title Extending QML - Signal Support Example -\brief Signal Support. -\ingroup qmlextendingexamples - -This example builds on: -\list -\li \l {Extending QML - Attached Properties Example} -\li \l {Extending QML - Grouped Properties Example} -\li \l {Extending QML - Default Property Example} -\li \l {Extending QML - Inheritance and Coercion Example} -\li \l {Extending QML - Object and List Property Types Example} -\li \l {Extending QML - Adding Types Example} -\endlist - -*/ - -/*! -\example referenceexamples/methods -\title Extending QML - Methods Example -\brief Methods Support. -\ingroup qmlextendingexamples - -This example builds on: -\list -\li \l {Extending QML - Inheritance and Coercion Example} -\li \l {Extending QML - Object and List Property Types Example} -\li \l {Extending QML - Adding Types Example} -\endlist - -The Methods Example has an additional method in the \c BirthdayParty class: \c invite(). -\c invite() is declared with \l Q_INVOKABLE so that it can be -called from QML. - -\snippet referenceexamples/methods/birthdayparty.h 0 - -In \c example.qml, the \c invite() method is called in the \l [QML]{QtQml::Component::completed()}{Component.onCompleted} signal handler: - -\snippet referenceexamples/methods/example.qml 0 -*/ - -/*! -\example referenceexamples/valuesource -\title Extending QML - Property Value Source Example -\brief Property Value Source. -\ingroup qmlextendingexamples - -This example builds on: -\list -\li \l {Extending QML - Signal Support Example} -\li \l {Extending QML - Attached Properties Example} -\li \l {Extending QML - Grouped Properties Example} -\li \l {Extending QML - Default Property Example} -\li \l {Extending QML - Inheritance and Coercion Example} -\li \l {Extending QML - Object and List Property Types Example} -\li \l {Extending QML - Adding Types Example} -\endlist - -*/ - -/*! -\example referenceexamples/binding -\title Extending QML - Binding Example -\brief Binding. -\ingroup qmlextendingexamples - -This example builds on: -\list -\li \l {Extending QML - Property Value Source Example} -\li \l {Extending QML - Signal Support Example} -\li \l {Extending QML - Attached Properties Example} -\li \l {Extending QML - Grouped Properties Example} -\li \l {Extending QML - Default Property Example} -\li \l {Extending QML - Inheritance and Coercion Example} -\li \l {Extending QML - Object and List Property Types Example} -\li \l {Extending QML - Adding Types Example} -\endlist - -*/ diff --git a/examples/qml/qml.pro b/examples/qml/qml.pro index 9fe8aab7e0..b59620cb81 100644 --- a/examples/qml/qml.pro +++ b/examples/qml/qml.pro @@ -7,7 +7,6 @@ qtHaveModule(quick) { } SUBDIRS += \ - referenceexamples \ tutorials EXAMPLE_FILES = \ diff --git a/examples/qml/referenceexamples/CMakeLists.txt b/examples/qml/referenceexamples/CMakeLists.txt deleted file mode 100644 index 88908ec9a0..0000000000 --- a/examples/qml/referenceexamples/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -qt_internal_add_example(adding) -qt_internal_add_example(coercion) -qt_internal_add_example(default) -qt_internal_add_example(properties) -qt_internal_add_example(methods) -if(TARGET Qt::Widgets) - qt_internal_add_example(extended) -endif() -if(TARGET Qt::Quick) - qt_internal_add_example(attached) - qt_internal_add_example(binding) - qt_internal_add_example(grouped) - qt_internal_add_example(signal) - qt_internal_add_example(valuesource) -endif() diff --git a/examples/qml/referenceexamples/adding/CMakeLists.txt b/examples/qml/referenceexamples/adding/CMakeLists.txt deleted file mode 100644 index f1d3fc6eac..0000000000 --- a/examples/qml/referenceexamples/adding/CMakeLists.txt +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(adding LANGUAGES CXX) - -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/adding") - -find_package(Qt6 REQUIRED COMPONENTS Core Qml) - -qt_add_executable(adding - main.cpp - person.cpp person.h -) - -set_target_properties(adding PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(adding PUBLIC - Qt::Core - Qt::Qml -) - -qt_add_qml_module(adding - URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH -) - -install(TARGETS adding - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/qml/referenceexamples/adding/adding.pro b/examples/qml/referenceexamples/adding/adding.pro deleted file mode 100644 index a4a677c3c4..0000000000 --- a/examples/qml/referenceexamples/adding/adding.pro +++ /dev/null @@ -1,13 +0,0 @@ -QT = core qml -CONFIG += qmltypes - -QML_IMPORT_NAME = People -QML_IMPORT_MAJOR_VERSION = 1 - -SOURCES += main.cpp \ - person.cpp -HEADERS += person.h -RESOURCES += adding.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/adding -INSTALLS += target diff --git a/examples/qml/referenceexamples/adding/adding.qrc b/examples/qml/referenceexamples/adding/adding.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/adding/adding.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/adding/example.qml b/examples/qml/referenceexamples/adding/example.qml deleted file mode 100644 index 068352bc35..0000000000 --- a/examples/qml/referenceexamples/adding/example.qml +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -// ![0] -import People - -Person { - name: "Bob Jones" - shoeSize: 12 -} -// ![0] diff --git a/examples/qml/referenceexamples/adding/main.cpp b/examples/qml/referenceexamples/adding/main.cpp deleted file mode 100644 index 319217da56..0000000000 --- a/examples/qml/referenceexamples/adding/main.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include <QCoreApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> -#include "person.h" - -int main(int argc, char ** argv) -{ - QCoreApplication app(argc, argv); - - QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *person = qobject_cast<Person *>(component.create()); - if (!person) { - qWarning() << component.errors(); - return EXIT_FAILURE; - } - - qInfo() << "The person's name is" << person->name() - << "\nThey wear a" << person->shoeSize() << "sized shoe"; - - return EXIT_SUCCESS; -} diff --git a/examples/qml/referenceexamples/adding/person.cpp b/examples/qml/referenceexamples/adding/person.cpp deleted file mode 100644 index 5b6ce4b9d6..0000000000 --- a/examples/qml/referenceexamples/adding/person.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -// ![0] -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - m_name = n; -} - -int Person::shoeSize() const -{ - return m_shoeSize; -} - -void Person::setShoeSize(int s) -{ - m_shoeSize = s; -} - -// ![0] diff --git a/examples/qml/referenceexamples/attached/attached.qrc b/examples/qml/referenceexamples/attached/attached.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/attached/attached.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/attached/birthdayparty.cpp b/examples/qml/referenceexamples/attached/birthdayparty.cpp deleted file mode 100644 index a76b0f55cb..0000000000 --- a/examples/qml/referenceexamples/attached/birthdayparty.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (C) 2021 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 d) -{ - m_rsvp = d; -} - -Person *BirthdayParty::host() const -{ - return m_host; -} - -void BirthdayParty::setHost(Person *c) -{ - m_host = c; -} - -QQmlListProperty<Person> BirthdayParty::guests() -{ - return {this, &m_guests}; -} - -qsizetype BirthdayParty::guestCount() const -{ - return m_guests.count(); -} - -Person *BirthdayParty::guest(qsizetype index) const -{ - return m_guests.at(index); -} - -BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) -{ - return new BirthdayPartyAttached(object); -} - diff --git a/examples/qml/referenceexamples/attached/birthdayparty.h b/examples/qml/referenceexamples/attached/birthdayparty.h deleted file mode 100644 index 7eb6508869..0000000000 --- a/examples/qml/referenceexamples/attached/birthdayparty.h +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef BIRTHDAYPARTY_H -#define BIRTHDAYPARTY_H - -#include <QObject> -#include <QDate> -#include <qqml.h> -#include "person.h" - -class BirthdayPartyAttached : public QObject -{ - Q_OBJECT - Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) - QML_ANONYMOUS -public: - using QObject::QObject; - - QDate rsvp() const; - void setRsvp(QDate); - -private: - QDate m_rsvp; -}; - -class BirthdayParty : public QObject -{ - Q_OBJECT - Q_PROPERTY(Person *host READ host WRITE setHost) - Q_PROPERTY(QQmlListProperty<Person> guests READ guests) - Q_CLASSINFO("DefaultProperty", "guests") - QML_ELEMENT - -//! [declare attached] - QML_ATTACHED(BirthdayPartyAttached) -//! [declare attached] - -public: - using QObject::QObject; - - Person *host() const; - void setHost(Person *); - - QQmlListProperty<Person> guests(); - qsizetype guestCount() const; - Person *guest(qsizetype) const; - - //! [static attached] - static BirthdayPartyAttached *qmlAttachedProperties(QObject *); - //! [static attached] -private: - Person *m_host = nullptr; - QList<Person *> m_guests; -}; - -#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/attached/person.cpp b/examples/qml/referenceexamples/attached/person.cpp deleted file mode 100644 index 358fbf0ed8..0000000000 --- a/examples/qml/referenceexamples/attached/person.cpp +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -int ShoeDescription::size() const -{ - return m_size; -} - -void ShoeDescription::setSize(int s) -{ - m_size = s; -} - -QColor ShoeDescription::color() const -{ - return m_color; -} - -void ShoeDescription::setColor(const QColor &c) -{ - m_color = c; -} - -QString ShoeDescription::brand() const -{ - return m_brand; -} - -void ShoeDescription::setBrand(const QString &b) -{ - m_brand = b; -} - -qreal ShoeDescription::price() const -{ - return m_price; -} - -void ShoeDescription::setPrice(qreal p) -{ - m_price = p; -} - -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - m_name = n; -} - -ShoeDescription *Person::shoe() -{ - return &m_shoe; -} diff --git a/examples/qml/referenceexamples/binding/CMakeLists.txt b/examples/qml/referenceexamples/binding/CMakeLists.txt deleted file mode 100644 index 83ad8001e8..0000000000 --- a/examples/qml/referenceexamples/binding/CMakeLists.txt +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(binding LANGUAGES CXX) - -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/binding") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml) - -qt_add_executable(binding - birthdayparty.cpp birthdayparty.h - happybirthdaysong.cpp happybirthdaysong.h - main.cpp - person.cpp person.h -) - -set_target_properties(binding PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(binding PUBLIC - Qt::Core - Qt::Gui - Qt::Qml -) - -qt_add_qml_module(binding - URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH -) - -install(TARGETS binding - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/qml/referenceexamples/binding/binding.pro b/examples/qml/referenceexamples/binding/binding.pro deleted file mode 100644 index dce780d0a2..0000000000 --- a/examples/qml/referenceexamples/binding/binding.pro +++ /dev/null @@ -1,17 +0,0 @@ -QT += qml - -CONFIG += qmltypes -QML_IMPORT_NAME = People -QML_IMPORT_MAJOR_VERSION = 1 - -SOURCES += main.cpp \ - person.cpp \ - birthdayparty.cpp \ - happybirthdaysong.cpp -HEADERS += person.h \ - birthdayparty.h \ - happybirthdaysong.h -RESOURCES += binding.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/binding -INSTALLS += target diff --git a/examples/qml/referenceexamples/binding/binding.qrc b/examples/qml/referenceexamples/binding/binding.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/binding/binding.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/binding/birthdayparty.cpp b/examples/qml/referenceexamples/binding/birthdayparty.cpp deleted file mode 100644 index a98b5fbdfb..0000000000 --- a/examples/qml/referenceexamples/binding/birthdayparty.cpp +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (C) 2021 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 d) -{ - if (d != m_rsvp) { - m_rsvp = d; - emit rsvpChanged(); - } -} - -Person *BirthdayParty::host() const -{ - return m_host; -} - -void BirthdayParty::setHost(Person *c) -{ - if (c == m_host) return; - m_host = c; - emit hostChanged(); -} - -QQmlListProperty<Person> BirthdayParty::guests() -{ - return QQmlListProperty<Person>(this, &m_guests); -} - -qsizetype BirthdayParty::guestCount() const -{ - return m_guests.count(); -} - -Person *BirthdayParty::guest(qsizetype index) const -{ - return m_guests.at(index); -} - -void BirthdayParty::startParty() -{ - QTime time = QTime::currentTime(); - emit partyStarted(time); -} - -QString BirthdayParty::announcement() const -{ - return QString(); -} - -void BirthdayParty::setAnnouncement(const QString &speak) -{ - qWarning().noquote() << speak; -} - -BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) -{ - return new BirthdayPartyAttached(object); -} - diff --git a/examples/qml/referenceexamples/binding/person.cpp b/examples/qml/referenceexamples/binding/person.cpp deleted file mode 100644 index a4c26679d6..0000000000 --- a/examples/qml/referenceexamples/binding/person.cpp +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -int ShoeDescription::size() const -{ - return m_size; -} - -void ShoeDescription::setSize(int s) -{ - if (m_size == s) - return; - - m_size = s; - emit shoeChanged(); -} - -QColor ShoeDescription::color() const -{ - return m_color; -} - -void ShoeDescription::setColor(const QColor &c) -{ - if (m_color == c) - return; - - m_color = c; - emit shoeChanged(); -} - -QString ShoeDescription::brand() const -{ - return m_brand; -} - -void ShoeDescription::setBrand(const QString &b) -{ - if (m_brand == b) - return; - - m_brand = b; - emit shoeChanged(); -} - -qreal ShoeDescription::price() const -{ - return m_price; -} - -void ShoeDescription::setPrice(qreal p) -{ - if (m_price == p) - return; - - m_price = p; - emit shoeChanged(); -} - -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - if (m_name == n) - return; - - m_name = n; - emit nameChanged(); -} - -ShoeDescription *Person::shoe() -{ - return &m_shoe; -} diff --git a/examples/qml/referenceexamples/coercion/birthdayparty.cpp b/examples/qml/referenceexamples/coercion/birthdayparty.cpp deleted file mode 100644 index 6cf8a608e7..0000000000 --- a/examples/qml/referenceexamples/coercion/birthdayparty.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "birthdayparty.h" - -Person *BirthdayParty::host() const -{ - return m_host; -} - -void BirthdayParty::setHost(Person *c) -{ - m_host = c; -} - -QQmlListProperty<Person> BirthdayParty::guests() -{ - return {this, &m_guests}; -} - -qsizetype BirthdayParty::guestCount() const -{ - return m_guests.count(); -} - -Person *BirthdayParty::guest(qsizetype index) const -{ - return m_guests.at(index); -} diff --git a/examples/qml/referenceexamples/coercion/birthdayparty.h b/examples/qml/referenceexamples/coercion/birthdayparty.h deleted file mode 100644 index 79508ff9c1..0000000000 --- a/examples/qml/referenceexamples/coercion/birthdayparty.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef BIRTHDAYPARTY_H -#define BIRTHDAYPARTY_H - -#include <QObject> -#include <QQmlListProperty> -#include "person.h" - -class BirthdayParty : public QObject -{ - Q_OBJECT -// ![0] - Q_PROPERTY(Person *host READ host WRITE setHost) - Q_PROPERTY(QQmlListProperty<Person> guests READ guests) - QML_ELEMENT -// ![0] -public: - using QObject::QObject; - - Person *host() const; - void setHost(Person *); - - QQmlListProperty<Person> guests(); - qsizetype guestCount() const; - Person *guest(qsizetype) const; - -private: - Person *m_host = nullptr; - QList<Person *> m_guests; -}; - -#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/coercion/coercion.qrc b/examples/qml/referenceexamples/coercion/coercion.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/coercion/coercion.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/coercion/person.cpp b/examples/qml/referenceexamples/coercion/person.cpp deleted file mode 100644 index ab7aefcbee..0000000000 --- a/examples/qml/referenceexamples/coercion/person.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - m_name = n; -} - -int Person::shoeSize() const -{ - return m_shoeSize; -} - -void Person::setShoeSize(int s) -{ - m_shoeSize = s; -} diff --git a/examples/qml/referenceexamples/default/birthdayparty.cpp b/examples/qml/referenceexamples/default/birthdayparty.cpp deleted file mode 100644 index 5a9f38d9f3..0000000000 --- a/examples/qml/referenceexamples/default/birthdayparty.cpp +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "birthdayparty.h" - -Person *BirthdayParty::host() const -{ - return m_host; -} - -void BirthdayParty::setHost(Person *c) -{ - m_host = c; -} - -QQmlListProperty<Person> BirthdayParty::guests() -{ - return {this, &m_guests}; -} - -qsizetype BirthdayParty::guestCount() const -{ - return m_guests.count(); -} - -Person *BirthdayParty::guest(qsizetype index) const -{ - return m_guests.at(index); -} - diff --git a/examples/qml/referenceexamples/default/birthdayparty.h b/examples/qml/referenceexamples/default/birthdayparty.h deleted file mode 100644 index ce5106ef99..0000000000 --- a/examples/qml/referenceexamples/default/birthdayparty.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef BIRTHDAYPARTY_H -#define BIRTHDAYPARTY_H - -#include <QObject> -#include <QQmlListProperty> -#include "person.h" - -// ![0] -class BirthdayParty : public QObject -{ - Q_OBJECT - Q_PROPERTY(Person *host READ host WRITE setHost) - Q_PROPERTY(QQmlListProperty<Person> guests READ guests) - Q_CLASSINFO("DefaultProperty", "guests") - QML_ELEMENT -public: - using QObject::QObject; - - Person *host() const; - void setHost(Person *); - - QQmlListProperty<Person> guests(); - qsizetype guestCount() const; - Person *guest(qsizetype) const; - -private: - Person *m_host = nullptr; - QList<Person *> m_guests; -}; -// ![0] - -#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/default/default.qrc b/examples/qml/referenceexamples/default/default.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/default/default.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/default/person.cpp b/examples/qml/referenceexamples/default/person.cpp deleted file mode 100644 index ab7aefcbee..0000000000 --- a/examples/qml/referenceexamples/default/person.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - m_name = n; -} - -int Person::shoeSize() const -{ - return m_shoeSize; -} - -void Person::setShoeSize(int s) -{ - m_shoeSize = s; -} diff --git a/examples/qml/referenceexamples/extended/CMakeLists.txt b/examples/qml/referenceexamples/extended/CMakeLists.txt deleted file mode 100644 index 6ecff60d0c..0000000000 --- a/examples/qml/referenceexamples/extended/CMakeLists.txt +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(extended LANGUAGES CXX) - -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/extended") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Widgets) - -qt_add_executable(extended - lineedit.cpp lineedit.h - main.cpp -) - -set_target_properties(extended PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(extended PUBLIC - Qt::Core - Qt::Gui - Qt::Qml - Qt::Widgets -) - -qt_add_qml_module(extended - URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH -) - -install(TARGETS extended - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/qml/referenceexamples/extended/example.qml b/examples/qml/referenceexamples/extended/example.qml deleted file mode 100644 index 8a0b3193d8..0000000000 --- a/examples/qml/referenceexamples/extended/example.qml +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -import People - -// ![0] -QLineEdit { - leftMargin: 20 -} -// ![0] diff --git a/examples/qml/referenceexamples/extended/extended.pro b/examples/qml/referenceexamples/extended/extended.pro deleted file mode 100644 index 094e5201ca..0000000000 --- a/examples/qml/referenceexamples/extended/extended.pro +++ /dev/null @@ -1,13 +0,0 @@ -QT += qml widgets - -CONFIG += qmltypes -QML_IMPORT_NAME = People -QML_IMPORT_MAJOR_VERSION = 1 - -SOURCES += main.cpp \ - lineedit.cpp -HEADERS += lineedit.h -RESOURCES += extended.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/extended -INSTALLS += target diff --git a/examples/qml/referenceexamples/extended/extended.qrc b/examples/qml/referenceexamples/extended/extended.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/extended/extended.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/extended/lineedit.cpp b/examples/qml/referenceexamples/extended/lineedit.cpp deleted file mode 100644 index 802f47ef03..0000000000 --- a/examples/qml/referenceexamples/extended/lineedit.cpp +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include "lineedit.h" -#include <qqml.h> - -LineEditExtension::LineEditExtension(QObject *object) -: QObject(object), m_lineedit(qobject_cast<QLineEdit *>(object)) -{ -} - -int LineEditExtension::leftMargin() const -{ - return m_lineedit->textMargins().left(); -} - -void LineEditExtension::setLeftMargin(int l) -{ - QMargins m = m_lineedit->textMargins(); - if (m.left() != l) { - m.setLeft(l); - m_lineedit->setTextMargins(m); - emit marginsChanged(); - } -} - -int LineEditExtension::rightMargin() const -{ - return m_lineedit->textMargins().right(); -} - -void LineEditExtension::setRightMargin(int r) -{ - QMargins m = m_lineedit->textMargins(); - if (m.right() != r) { - m.setRight(r); - m_lineedit->setTextMargins(m); - emit marginsChanged(); - } -} - -int LineEditExtension::topMargin() const -{ - return m_lineedit->textMargins().top(); -} - -void LineEditExtension::setTopMargin(int t) -{ - QMargins m = m_lineedit->textMargins(); - if (m.top() != t) { - m.setTop(t); - m_lineedit->setTextMargins(m); - emit marginsChanged(); - } -} - -int LineEditExtension::bottomMargin() const -{ - return m_lineedit->textMargins().bottom(); -} - -void LineEditExtension::setBottomMargin(int b) -{ - QMargins m = m_lineedit->textMargins(); - if (m.bottom() != b) { - m.setBottom(b); - m_lineedit->setTextMargins(m); - emit marginsChanged(); - } -} - - diff --git a/examples/qml/referenceexamples/extended/lineedit.h b/examples/qml/referenceexamples/extended/lineedit.h deleted file mode 100644 index 44bb9fe72d..0000000000 --- a/examples/qml/referenceexamples/extended/lineedit.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#ifndef LINEEDIT_H -#define LINEEDIT_H - -#include <QLineEdit> -#include <qqml.h> - -class LineEditExtension : public QObject -{ - Q_OBJECT - Q_PROPERTY(int leftMargin READ leftMargin WRITE setLeftMargin NOTIFY marginsChanged) - Q_PROPERTY(int rightMargin READ rightMargin WRITE setRightMargin NOTIFY marginsChanged) - Q_PROPERTY(int topMargin READ topMargin WRITE setTopMargin NOTIFY marginsChanged) - Q_PROPERTY(int bottomMargin READ bottomMargin WRITE setBottomMargin NOTIFY marginsChanged) -public: - LineEditExtension(QObject *); - - int leftMargin() const; - void setLeftMargin(int); - - int rightMargin() const; - void setRightMargin(int); - - int topMargin() const; - void setTopMargin(int); - - int bottomMargin() const; - void setBottomMargin(int); -signals: - void marginsChanged(); - -private: - QLineEdit *m_lineedit; -}; - -// ![0] -struct QLineEditForeign -{ - Q_GADGET - QML_FOREIGN(QLineEdit) - QML_NAMED_ELEMENT(QLineEdit) - QML_EXTENDED(LineEditExtension) -}; -// ![0] - -#endif // LINEEDIT_H diff --git a/examples/qml/referenceexamples/extended/main.cpp b/examples/qml/referenceexamples/extended/main.cpp deleted file mode 100644 index 7861acf707..0000000000 --- a/examples/qml/referenceexamples/extended/main.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> -#include <QLineEdit> -#include "lineedit.h" - -int main(int argc, char ** argv) -{ - QApplication app(argc, argv); - -// ![1] - QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *edit = qobject_cast<QLineEdit *>(component.create()); -// ![1] - - if (edit) { - edit->show(); - return QApplication::exec(); - } - - qWarning() << component.errors(); - return EXIT_FAILURE; -} diff --git a/examples/qml/referenceexamples/grouped/birthdayparty.cpp b/examples/qml/referenceexamples/grouped/birthdayparty.cpp deleted file mode 100644 index 6cf8a608e7..0000000000 --- a/examples/qml/referenceexamples/grouped/birthdayparty.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "birthdayparty.h" - -Person *BirthdayParty::host() const -{ - return m_host; -} - -void BirthdayParty::setHost(Person *c) -{ - m_host = c; -} - -QQmlListProperty<Person> BirthdayParty::guests() -{ - return {this, &m_guests}; -} - -qsizetype BirthdayParty::guestCount() const -{ - return m_guests.count(); -} - -Person *BirthdayParty::guest(qsizetype index) const -{ - return m_guests.at(index); -} diff --git a/examples/qml/referenceexamples/grouped/birthdayparty.h b/examples/qml/referenceexamples/grouped/birthdayparty.h deleted file mode 100644 index 7f011ca95f..0000000000 --- a/examples/qml/referenceexamples/grouped/birthdayparty.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef BIRTHDAYPARTY_H -#define BIRTHDAYPARTY_H - -#include <QObject> -#include <QQmlListProperty> -#include "person.h" - -class BirthdayParty : public QObject -{ - Q_OBJECT - Q_PROPERTY(Person *host READ host WRITE setHost) - Q_PROPERTY(QQmlListProperty<Person> guests READ guests) - Q_CLASSINFO("DefaultProperty", "guests") - QML_ELEMENT -public: - using QObject::QObject; - - Person *host() const; - void setHost(Person *); - - QQmlListProperty<Person> guests(); - qsizetype guestCount() const; - Person *guest(qsizetype) const; - -private: - Person *m_host; - QList<Person *> m_guests; -}; - - -#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/grouped/grouped.qrc b/examples/qml/referenceexamples/grouped/grouped.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/grouped/grouped.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/grouped/person.cpp b/examples/qml/referenceexamples/grouped/person.cpp deleted file mode 100644 index 358fbf0ed8..0000000000 --- a/examples/qml/referenceexamples/grouped/person.cpp +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -int ShoeDescription::size() const -{ - return m_size; -} - -void ShoeDescription::setSize(int s) -{ - m_size = s; -} - -QColor ShoeDescription::color() const -{ - return m_color; -} - -void ShoeDescription::setColor(const QColor &c) -{ - m_color = c; -} - -QString ShoeDescription::brand() const -{ - return m_brand; -} - -void ShoeDescription::setBrand(const QString &b) -{ - m_brand = b; -} - -qreal ShoeDescription::price() const -{ - return m_price; -} - -void ShoeDescription::setPrice(qreal p) -{ - m_price = p; -} - -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - m_name = n; -} - -ShoeDescription *Person::shoe() -{ - return &m_shoe; -} diff --git a/examples/qml/referenceexamples/methods/birthdayparty.cpp b/examples/qml/referenceexamples/methods/birthdayparty.cpp deleted file mode 100644 index 35ae42f779..0000000000 --- a/examples/qml/referenceexamples/methods/birthdayparty.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "birthdayparty.h" - -// ![0] -Person *BirthdayParty::host() const -{ - return m_host; -} - -void BirthdayParty::setHost(Person *c) -{ - m_host = c; -} - -QQmlListProperty<Person> BirthdayParty::guests() -{ - return {this, &m_guests}; -} - -qsizetype BirthdayParty::guestCount() const -{ - return m_guests.count(); -} - -Person *BirthdayParty::guest(qsizetype index) const -{ - return m_guests.at(index); -} - -void BirthdayParty::invite(const QString &name) -{ - auto *person = new Person(this); - person->setName(name); - m_guests.append(person); -} -// ![0] - diff --git a/examples/qml/referenceexamples/methods/birthdayparty.h b/examples/qml/referenceexamples/methods/birthdayparty.h deleted file mode 100644 index 796464c333..0000000000 --- a/examples/qml/referenceexamples/methods/birthdayparty.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef BIRTHDAYPARTY_H -#define BIRTHDAYPARTY_H - -#include <QObject> -#include <QQmlListProperty> -#include "person.h" - -class BirthdayParty : public QObject -{ - Q_OBJECT - Q_PROPERTY(Person *host READ host WRITE setHost) - Q_PROPERTY(QQmlListProperty<Person> guests READ guests) - QML_ELEMENT -public: - using QObject::QObject; - - Person *host() const; - void setHost(Person *); - - QQmlListProperty<Person> guests(); - qsizetype guestCount() const; - Person *guest(qsizetype) const; - -// ![0] - Q_INVOKABLE void invite(const QString &name); -// ![0] - -private: - Person *m_host = nullptr; - QList<Person *> m_guests; -}; - -#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/methods/example.qml b/examples/qml/referenceexamples/methods/example.qml deleted file mode 100644 index 41d053edd2..0000000000 --- a/examples/qml/referenceexamples/methods/example.qml +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -// ![0] -import QtQuick -import People - -BirthdayParty { - host: Person { - name: "Bob Jones" - shoeSize: 12 - } - guests: [ - Person { name: "Leo Hodges" }, - Person { name: "Jack Smith" }, - Person { name: "Anne Brown" } - ] - - Component.onCompleted: invite("William Green") -} -// ![0] diff --git a/examples/qml/referenceexamples/methods/methods.qrc b/examples/qml/referenceexamples/methods/methods.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/methods/methods.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/methods/person.cpp b/examples/qml/referenceexamples/methods/person.cpp deleted file mode 100644 index ab7aefcbee..0000000000 --- a/examples/qml/referenceexamples/methods/person.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - m_name = n; -} - -int Person::shoeSize() const -{ - return m_shoeSize; -} - -void Person::setShoeSize(int s) -{ - m_shoeSize = s; -} diff --git a/examples/qml/referenceexamples/methods/person.h b/examples/qml/referenceexamples/methods/person.h deleted file mode 100644 index d8d4941183..0000000000 --- a/examples/qml/referenceexamples/methods/person.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef PERSON_H -#define PERSON_H - -#include <QObject> -#include <QtQml/qqml.h> - -class Person : public QObject -{ - Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) - QML_ELEMENT -public: - using QObject::QObject; - - QString name() const; - void setName(const QString &); - - int shoeSize() const; - void setShoeSize(int); - -private: - QString m_name; - int m_shoeSize = 0; -}; - -#endif // PERSON_H diff --git a/examples/qml/referenceexamples/properties/CMakeLists.txt b/examples/qml/referenceexamples/properties/CMakeLists.txt deleted file mode 100644 index dbe521d17d..0000000000 --- a/examples/qml/referenceexamples/properties/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(properties LANGUAGES CXX) - -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/properties") - -find_package(Qt6 REQUIRED COMPONENTS Core Qml) - -qt_add_executable(properties - birthdayparty.cpp birthdayparty.h - main.cpp - person.cpp person.h -) - -set_target_properties(properties PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(properties PUBLIC - Qt::Core - Qt::Qml -) - -qt_add_qml_module(properties - URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH -) - -install(TARGETS properties - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/qml/referenceexamples/properties/birthdayparty.cpp b/examples/qml/referenceexamples/properties/birthdayparty.cpp deleted file mode 100644 index 2f97634abc..0000000000 --- a/examples/qml/referenceexamples/properties/birthdayparty.cpp +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "birthdayparty.h" - -// ![0] -Person *BirthdayParty::host() const -{ - return m_host; -} - -void BirthdayParty::setHost(Person *c) -{ - m_host = c; -} - -QQmlListProperty<Person> BirthdayParty::guests() -{ - return {this, this, - &BirthdayParty::appendGuest, - &BirthdayParty::guestCount, - &BirthdayParty::guest, - &BirthdayParty::clearGuests, - &BirthdayParty::replaceGuest, - &BirthdayParty::removeLastGuest}; -} - -void BirthdayParty::appendGuest(Person *p) -{ - m_guests.append(p); -} - -qsizetype BirthdayParty::guestCount() const -{ - return m_guests.count(); -} - -Person *BirthdayParty::guest(qsizetype index) const -{ - return m_guests.at(index); -} - -void BirthdayParty::clearGuests() { - m_guests.clear(); -} - -void BirthdayParty::replaceGuest(qsizetype index, Person *p) -{ - m_guests[index] = p; -} - -void BirthdayParty::removeLastGuest() -{ - m_guests.removeLast(); -} - -// ![0] - -void BirthdayParty::appendGuest(QQmlListProperty<Person> *list, Person *p) -{ - reinterpret_cast< BirthdayParty *>(list->data)->appendGuest(p); -} - -void BirthdayParty::clearGuests(QQmlListProperty<Person>* list) -{ - reinterpret_cast< BirthdayParty *>(list->data)->clearGuests(); -} - -void BirthdayParty::replaceGuest(QQmlListProperty<Person> *list, qsizetype i, Person *p) -{ - reinterpret_cast< BirthdayParty* >(list->data)->replaceGuest(i, p); -} - -void BirthdayParty::removeLastGuest(QQmlListProperty<Person> *list) -{ - reinterpret_cast< BirthdayParty* >(list->data)->removeLastGuest(); -} - -Person* BirthdayParty::guest(QQmlListProperty<Person> *list, qsizetype i) -{ - return reinterpret_cast< BirthdayParty* >(list->data)->guest(i); -} - -qsizetype BirthdayParty::guestCount(QQmlListProperty<Person> *list) -{ - return reinterpret_cast< BirthdayParty* >(list->data)->guestCount(); -} diff --git a/examples/qml/referenceexamples/properties/main.cpp b/examples/qml/referenceexamples/properties/main.cpp deleted file mode 100644 index f381686dbc..0000000000 --- a/examples/qml/referenceexamples/properties/main.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QCoreApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> -#include "birthdayparty.h" -#include "person.h" - -int main(int argc, char ** argv) -{ - QCoreApplication app(argc, argv); - - QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *party = qobject_cast<BirthdayParty *>(component.create()); - - if (party && party->host()) { - qInfo() << party->host()->name() << "is having a birthday!\n" - "They are inviting:"; - for (qsizetype ii = 0; ii < party->guestCount(); ++ii) - qInfo() << " " << party->guest(ii)->name(); - return EXIT_SUCCESS; - } - - qWarning() << component.errors(); - return EXIT_FAILURE; -} diff --git a/examples/qml/referenceexamples/properties/person.cpp b/examples/qml/referenceexamples/properties/person.cpp deleted file mode 100644 index ab7aefcbee..0000000000 --- a/examples/qml/referenceexamples/properties/person.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - m_name = n; -} - -int Person::shoeSize() const -{ - return m_shoeSize; -} - -void Person::setShoeSize(int s) -{ - m_shoeSize = s; -} diff --git a/examples/qml/referenceexamples/properties/person.h b/examples/qml/referenceexamples/properties/person.h deleted file mode 100644 index d8d4941183..0000000000 --- a/examples/qml/referenceexamples/properties/person.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef PERSON_H -#define PERSON_H - -#include <QObject> -#include <QtQml/qqml.h> - -class Person : public QObject -{ - Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) - QML_ELEMENT -public: - using QObject::QObject; - - QString name() const; - void setName(const QString &); - - int shoeSize() const; - void setShoeSize(int); - -private: - QString m_name; - int m_shoeSize = 0; -}; - -#endif // PERSON_H diff --git a/examples/qml/referenceexamples/properties/properties.pro b/examples/qml/referenceexamples/properties/properties.pro deleted file mode 100644 index 6697afa2c5..0000000000 --- a/examples/qml/referenceexamples/properties/properties.pro +++ /dev/null @@ -1,15 +0,0 @@ -QT = core qml - -CONFIG += qmltypes -QML_IMPORT_NAME = People -QML_IMPORT_MAJOR_VERSION = 1 - -SOURCES += main.cpp \ - person.cpp \ - birthdayparty.cpp -HEADERS += person.h \ - birthdayparty.h -RESOURCES += properties.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/properties -INSTALLS += target diff --git a/examples/qml/referenceexamples/properties/properties.qrc b/examples/qml/referenceexamples/properties/properties.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/properties/properties.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/referenceexamples.pro b/examples/qml/referenceexamples/referenceexamples.pro deleted file mode 100644 index 3f4bbcf75d..0000000000 --- a/examples/qml/referenceexamples/referenceexamples.pro +++ /dev/null @@ -1,17 +0,0 @@ -TEMPLATE = subdirs - -SUBDIRS += \ - adding \ - coercion \ - default \ - properties \ - methods - -qtHaveModule(widgets): SUBDIRS += extended - -qtHaveModule(quick): SUBDIRS += \ - attached \ - binding \ - grouped \ - signal \ - valuesource diff --git a/examples/qml/referenceexamples/referenceexamples.qmlproject b/examples/qml/referenceexamples/referenceexamples.qmlproject deleted file mode 100644 index 2bb4016996..0000000000 --- a/examples/qml/referenceexamples/referenceexamples.qmlproject +++ /dev/null @@ -1,14 +0,0 @@ -import QmlProject 1.0 - -Project { - /* Include .qml, .js, and image files from current directory and subdirectories */ - QmlFiles { - directory: "." - } - JavaScriptFiles { - directory: "." - } - ImageFiles { - directory: "." - } -} diff --git a/examples/qml/referenceexamples/signal/CMakeLists.txt b/examples/qml/referenceexamples/signal/CMakeLists.txt deleted file mode 100644 index ce196f3a38..0000000000 --- a/examples/qml/referenceexamples/signal/CMakeLists.txt +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(signal LANGUAGES CXX) - -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/signal") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml) - -qt_add_executable(signal - birthdayparty.cpp birthdayparty.h - main.cpp - person.cpp person.h -) - -set_target_properties(signal PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(signal PUBLIC - Qt::Core - Qt::Gui - Qt::Qml -) - -qt_add_qml_module(signal - URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH -) - -install(TARGETS signal - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/qml/referenceexamples/signal/birthdayparty.cpp b/examples/qml/referenceexamples/signal/birthdayparty.cpp deleted file mode 100644 index b2cc131597..0000000000 --- a/examples/qml/referenceexamples/signal/birthdayparty.cpp +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (C) 2021 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 d) -{ - m_rsvp = d; -} - -Person *BirthdayParty::host() const -{ - return m_host; -} - -void BirthdayParty::setHost(Person *c) -{ - m_host = c; -} - -QQmlListProperty<Person> BirthdayParty::guests() -{ - return {this, &m_guests}; -} - -qsizetype BirthdayParty::guestCount() const -{ - return m_guests.count(); -} - -Person *BirthdayParty::guest(qsizetype index) const -{ - return m_guests.at(index); -} - -void BirthdayParty::startParty() -{ - QTime time = QTime::currentTime(); - emit partyStarted(time); -} - -BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) -{ - return new BirthdayPartyAttached(object); -} - diff --git a/examples/qml/referenceexamples/signal/birthdayparty.h b/examples/qml/referenceexamples/signal/birthdayparty.h deleted file mode 100644 index 22f3bdf9da..0000000000 --- a/examples/qml/referenceexamples/signal/birthdayparty.h +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#ifndef BIRTHDAYPARTY_H -#define BIRTHDAYPARTY_H - -#include <QObject> -#include <QDate> -#include <qqml.h> -#include "person.h" - -class BirthdayPartyAttached : public QObject -{ - Q_OBJECT - Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) - QML_ANONYMOUS -public: - using QObject::QObject; - - QDate rsvp() const; - void setRsvp(QDate); - -private: - QDate m_rsvp; -}; - -class BirthdayParty : public QObject -{ - Q_OBJECT - Q_PROPERTY(Person *host READ host WRITE setHost) - Q_PROPERTY(QQmlListProperty<Person> guests READ guests) - Q_CLASSINFO("DefaultProperty", "guests") - QML_ELEMENT - QML_ATTACHED(BirthdayPartyAttached) -public: - using QObject::QObject; - - Person *host() const; - void setHost(Person *); - - QQmlListProperty<Person> guests(); - qsizetype guestCount() const; - Person *guest(qsizetype) const; - - static BirthdayPartyAttached *qmlAttachedProperties(QObject *); - - void startParty(); -// ![0] -signals: - void partyStarted(QTime time); -// ![0] - -private: - Person *m_host = nullptr; - QList<Person *> m_guests; -}; - -#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/signal/example.qml b/examples/qml/referenceexamples/signal/example.qml deleted file mode 100644 index c35db2fe46..0000000000 --- a/examples/qml/referenceexamples/signal/example.qml +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -import People -import QtQuick // For QColor - -BirthdayParty { -// ![0] - onPartyStarted: (time) => { console.log("This party started rockin' at " + time); } -// ![0] - - host: Boy { - name: "Bob Jones" - shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } - } - - Boy { - name: "Leo Hodges" - BirthdayParty.rsvp: "2009-07-06" - shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } - } - Boy { - name: "Jack Smith" - shoe { size: 8; color: "blue"; brand: "Puma"; price: 19.95 } - } - Girl { - name: "Anne Brown" - BirthdayParty.rsvp: "2009-07-01" - shoe.size: 7 - shoe.color: "red" - shoe.brand: "Marc Jacobs" - shoe.price: 699.99 - } -// ![1] -} -// ![1] diff --git a/examples/qml/referenceexamples/signal/main.cpp b/examples/qml/referenceexamples/signal/main.cpp deleted file mode 100644 index f99ddeb6c7..0000000000 --- a/examples/qml/referenceexamples/signal/main.cpp +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QCoreApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> -#include "birthdayparty.h" -#include "person.h" - -int main(int argc, char ** argv) -{ - QCoreApplication app(argc, argv); - - QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *party = qobject_cast<BirthdayParty *>(component.create()); - - if (party && party->host()) { - qInfo() << party->host()->name() << "is having a birthday!"; - - if (qobject_cast<Boy *>(party->host())) - qInfo() << "He is inviting:"; - else - qInfo() << "She is inviting:"; - - for (qsizetype ii = 0; ii < party->guestCount(); ++ii) { - Person *guest = party->guest(ii); - - QDate rsvpDate; - QObject *attached = - qmlAttachedPropertiesObject<BirthdayParty>(guest, false); - if (attached) - rsvpDate = attached->property("rsvp").toDate(); - - if (rsvpDate.isNull()) - qInfo() << " " << guest->name() << "RSVP date: Hasn't RSVP'd"; - else - qInfo() << " " << guest->name() << "RSVP date:" << rsvpDate.toString(); - } - - party->startParty(); - return EXIT_SUCCESS; - } - - qWarning() << component.errors(); - return EXIT_FAILURE; -} diff --git a/examples/qml/referenceexamples/signal/person.cpp b/examples/qml/referenceexamples/signal/person.cpp deleted file mode 100644 index 358fbf0ed8..0000000000 --- a/examples/qml/referenceexamples/signal/person.cpp +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -int ShoeDescription::size() const -{ - return m_size; -} - -void ShoeDescription::setSize(int s) -{ - m_size = s; -} - -QColor ShoeDescription::color() const -{ - return m_color; -} - -void ShoeDescription::setColor(const QColor &c) -{ - m_color = c; -} - -QString ShoeDescription::brand() const -{ - return m_brand; -} - -void ShoeDescription::setBrand(const QString &b) -{ - m_brand = b; -} - -qreal ShoeDescription::price() const -{ - return m_price; -} - -void ShoeDescription::setPrice(qreal p) -{ - m_price = p; -} - -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - m_name = n; -} - -ShoeDescription *Person::shoe() -{ - return &m_shoe; -} diff --git a/examples/qml/referenceexamples/signal/signal.pro b/examples/qml/referenceexamples/signal/signal.pro deleted file mode 100644 index 3c31234b3c..0000000000 --- a/examples/qml/referenceexamples/signal/signal.pro +++ /dev/null @@ -1,15 +0,0 @@ -QT += qml - -CONFIG += qmltypes -QML_IMPORT_NAME = People -QML_IMPORT_MAJOR_VERSION = 1 - -SOURCES += main.cpp \ - person.cpp \ - birthdayparty.cpp -HEADERS += person.h \ - birthdayparty.h -RESOURCES += signal.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/signal -INSTALLS += target diff --git a/examples/qml/referenceexamples/signal/signal.qrc b/examples/qml/referenceexamples/signal/signal.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/signal/signal.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/referenceexamples/valuesource/birthdayparty.cpp b/examples/qml/referenceexamples/valuesource/birthdayparty.cpp deleted file mode 100644 index ba8b8829e7..0000000000 --- a/examples/qml/referenceexamples/valuesource/birthdayparty.cpp +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (C) 2021 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 d) -{ - m_rsvp = d; -} - -Person *BirthdayParty::host() const -{ - return m_host; -} - -void BirthdayParty::setHost(Person *c) -{ - m_host = c; -} - -QQmlListProperty<Person> BirthdayParty::guests() -{ - return {this, &m_guests}; -} - -qsizetype BirthdayParty::guestCount() const -{ - return m_guests.count(); -} - -Person *BirthdayParty::guest(qsizetype index) const -{ - return m_guests.at(index); -} - -void BirthdayParty::startParty() -{ - QTime time = QTime::currentTime(); - emit partyStarted(time); -} - -QString BirthdayParty::announcement() const -{ - return QString(); -} - -void BirthdayParty::setAnnouncement(const QString &speak) -{ - qInfo().noquote() << speak; -} - -BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) -{ - return new BirthdayPartyAttached(object); -} diff --git a/examples/qml/referenceexamples/valuesource/person.cpp b/examples/qml/referenceexamples/valuesource/person.cpp deleted file mode 100644 index 358fbf0ed8..0000000000 --- a/examples/qml/referenceexamples/valuesource/person.cpp +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "person.h" - -int ShoeDescription::size() const -{ - return m_size; -} - -void ShoeDescription::setSize(int s) -{ - m_size = s; -} - -QColor ShoeDescription::color() const -{ - return m_color; -} - -void ShoeDescription::setColor(const QColor &c) -{ - m_color = c; -} - -QString ShoeDescription::brand() const -{ - return m_brand; -} - -void ShoeDescription::setBrand(const QString &b) -{ - m_brand = b; -} - -qreal ShoeDescription::price() const -{ - return m_price; -} - -void ShoeDescription::setPrice(qreal p) -{ - m_price = p; -} - -QString Person::name() const -{ - return m_name; -} - -void Person::setName(const QString &n) -{ - m_name = n; -} - -ShoeDescription *Person::shoe() -{ - return &m_shoe; -} diff --git a/examples/qml/referenceexamples/valuesource/person.h b/examples/qml/referenceexamples/valuesource/person.h deleted file mode 100644 index b5fa469025..0000000000 --- a/examples/qml/referenceexamples/valuesource/person.h +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef PERSON_H -#define PERSON_H - -#include <QObject> -#include <QColor> -#include <QtQml/qqml.h> - -class ShoeDescription : public QObject -{ - Q_OBJECT - Q_PROPERTY(int size READ size WRITE setSize) - Q_PROPERTY(QColor color READ color WRITE setColor) - Q_PROPERTY(QString brand READ brand WRITE setBrand) - Q_PROPERTY(qreal price READ price WRITE setPrice) - QML_ANONYMOUS -public: - using QObject::QObject; - - int size() const; - void setSize(int); - - QColor color() const; - void setColor(const QColor &); - - QString brand() const; - void setBrand(const QString &); - - qreal price() const; - void setPrice(qreal); - -private: - int m_size = 0; - QColor m_color; - QString m_brand; - qreal m_price = 0; -}; - -class Person : public QObject -{ - Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) -// ![1] - Q_PROPERTY(ShoeDescription *shoe READ shoe) -// ![1] - QML_ANONYMOUS -public: - using QObject::QObject; - - QString name() const; - void setName(const QString &); - - ShoeDescription *shoe(); -private: - QString m_name; - ShoeDescription m_shoe; -}; - -class Boy : public Person -{ - Q_OBJECT - QML_ELEMENT -public: - using Person::Person; -}; - -class Girl : public Person -{ - Q_OBJECT - QML_ELEMENT -public: - using Person::Person; -}; - -#endif // PERSON_H diff --git a/examples/qml/referenceexamples/valuesource/valuesource.qrc b/examples/qml/referenceexamples/valuesource/valuesource.qrc deleted file mode 100644 index e2fa01d5e7..0000000000 --- a/examples/qml/referenceexamples/valuesource/valuesource.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource> - <file>example.qml</file> -</qresource> -</RCC> diff --git a/examples/qml/tutorials/extending-qml-advanced/CMakeLists.txt b/examples/qml/tutorials/extending-qml-advanced/CMakeLists.txt new file mode 100644 index 0000000000..5541a8761d --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +qt_internal_add_example(advanced1-Base-project) +qt_internal_add_example(advanced2-Inheritance-and-coercion) +qt_internal_add_example(advanced3-Default-properties) +qt_internal_add_example(advanced4-Grouped-properties) +qt_internal_add_example(advanced5-Attached-properties) +qt_internal_add_example(advanced6-Property-value-source) +qt_internal_add_example(advanced7-Extension-objects) diff --git a/examples/qml/referenceexamples/methods/CMakeLists.txt b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/CMakeLists.txt index 57c0733723..a8064b77ef 100644 --- a/examples/qml/referenceexamples/methods/CMakeLists.txt +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/CMakeLists.txt @@ -2,41 +2,41 @@ # SPDX-License-Identifier: BSD-3-Clause cmake_minimum_required(VERSION 3.16) -project(methods LANGUAGES CXX) - -set(CMAKE_AUTOMOC ON) +project(baseproject LANGUAGES CXX) if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/methods") +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/tutorials/extending-qml-advanced/advanced1-Basic-project") find_package(Qt6 REQUIRED COMPONENTS Core Qml) +qt_standard_project_setup() + +qt_policy(SET QTP0001 NEW) -qt_add_executable(methods +qt_add_executable(baseproject birthdayparty.cpp birthdayparty.h main.cpp person.cpp person.h ) -set_target_properties(methods PROPERTIES +set_target_properties(baseproject PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE ) -target_link_libraries(methods PUBLIC +target_link_libraries(baseproject PUBLIC Qt::Core Qt::Qml ) -qt_add_qml_module(methods +qt_add_qml_module(baseproject URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH + QML_FILES Main.qml ) -install(TARGETS methods +install(TARGETS baseproject RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" diff --git a/examples/qml/referenceexamples/properties/example.qml b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/Main.qml index 52fb3344f0..c917d5b1f1 100644 --- a/examples/qml/referenceexamples/properties/example.qml +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/Main.qml @@ -1,9 +1,8 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import People -// ![0] BirthdayParty { host: Person { name: "Bob Jones" @@ -15,4 +14,3 @@ BirthdayParty { Person { name: "Anne Brown" } ] } -// ![0] diff --git a/examples/qml/referenceexamples/methods/methods.pro b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/baseproject.pro index 2a5f3cff41..26c865fbba 100644 --- a/examples/qml/referenceexamples/methods/methods.pro +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/baseproject.pro @@ -9,7 +9,5 @@ SOURCES += main.cpp \ birthdayparty.cpp HEADERS += person.h \ birthdayparty.h -RESOURCES += methods.qrc -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/methods -INSTALLS += target +RESOURCES += baseproject.qrc diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/baseproject.qrc b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/baseproject.qrc new file mode 100644 index 0000000000..b1eeb489e2 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/baseproject.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/qt/qml/People/"> + <file>Main.qml</file> + <file alias="qmldir">qmldir.in</file> +</qresource> +</RCC> diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/birthdayparty.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/birthdayparty.cpp new file mode 100644 index 0000000000..ad38f284e7 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/birthdayparty.cpp @@ -0,0 +1,99 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "birthdayparty.h" + +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(); +} diff --git a/examples/qml/referenceexamples/properties/birthdayparty.h b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/birthdayparty.h index 239626da17..1a28ef2632 100644 --- a/examples/qml/referenceexamples/properties/birthdayparty.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/birthdayparty.h @@ -1,26 +1,19 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H +#include "person.h" + #include <QObject> -#include <QList> #include <QQmlListProperty> -#include "person.h" -// ![0] class BirthdayParty : public QObject { Q_OBJECT -// ![0] -// ![1] - Q_PROPERTY(Person *host READ host WRITE setHost) -// ![1] -// ![2] - Q_PROPERTY(QQmlListProperty<Person> guests READ guests) -// ![2] -// ![3] + Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged FINAL) + Q_PROPERTY(QQmlListProperty<Person> guests READ guests NOTIFY guestsChanged FINAL) QML_ELEMENT public: using QObject::QObject; @@ -36,10 +29,14 @@ public: void replaceGuest(qsizetype, Person *); void removeLastGuest(); +signals: + void hostChanged(); + void guestsChanged(); + private: static void appendGuest(QQmlListProperty<Person> *, Person *); static qsizetype guestCount(QQmlListProperty<Person> *); - static Person* guest(QQmlListProperty<Person> *, qsizetype); + static Person *guest(QQmlListProperty<Person> *, qsizetype); static void clearGuests(QQmlListProperty<Person> *); static void replaceGuest(QQmlListProperty<Person> *, qsizetype, Person *); static void removeLastGuest(QQmlListProperty<Person> *); @@ -47,6 +44,5 @@ private: Person *m_host = nullptr; QList<Person *> m_guests; }; -// ![3] #endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/methods/main.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/main.cpp index dd9b03566b..eac94016d2 100644 --- a/examples/qml/referenceexamples/methods/main.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/main.cpp @@ -1,24 +1,27 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QCoreApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> #include "birthdayparty.h" #include "person.h" -int main(int argc, char ** argv) +#include <QCoreApplication> +#include <QDebug> +#include <QQmlComponent> +#include <QQmlEngine> + +int main(int argc, char **argv) { QCoreApplication app(argc, argv); QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *party = qobject_cast<BirthdayParty *>(component.create()); + QQmlComponent component(&engine); + component.loadFromModule("People", "Main"); + std::unique_ptr<BirthdayParty> party{ qobject_cast<BirthdayParty *>(component.create()) }; if (party && party->host()) { - qInfo() << party->host()->name() << "is having a birthday!" - << "\nThey are inviting:"; + qInfo() << party->host()->name() + << "is having a birthday!\n" + "They are inviting:"; for (qsizetype ii = 0; ii < party->guestCount(); ++ii) qInfo() << " " << party->guest(ii)->name(); return EXIT_SUCCESS; diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/person.cpp new file mode 100644 index 0000000000..f8f4b1d2f4 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/person.cpp @@ -0,0 +1,30 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "person.h" + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &name) +{ + if (m_name != name) { + m_name = name; + emit nameChanged(); + } +} + +int Person::shoeSize() const +{ + return m_shoeSize; +} + +void Person::setShoeSize(int shoeSize) +{ + if (m_shoeSize != shoeSize) { + m_shoeSize = shoeSize; + emit shoeSizeChanged(); + } +} diff --git a/examples/qml/referenceexamples/adding/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/person.h index 867927ee2b..ded272626a 100644 --- a/examples/qml/referenceexamples/adding/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/person.h @@ -1,18 +1,17 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef PERSON_H #define PERSON_H -#include <QObject> #include <QtQml/qqml.h> +#include <QObject> -//![0] class Person : public QObject { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) + Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize NOTIFY shoeSizeChanged FINAL) QML_ELEMENT public: using QObject::QObject; @@ -23,10 +22,13 @@ public: int shoeSize() const; void setShoeSize(int); +signals: + void nameChanged(); + void shoeSizeChanged(); + private: QString m_name; int m_shoeSize = 0; }; -//![0] #endif // PERSON_H diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/qmldir.in b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/qmldir.in new file mode 100644 index 0000000000..70cde3c958 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced1-Base-project/qmldir.in @@ -0,0 +1,4 @@ +module People +typeinfo birthdayparty.qmltypes +prefer :/qt/qml/People/ +Main 254.0 Main.qml diff --git a/examples/qml/referenceexamples/coercion/CMakeLists.txt b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/CMakeLists.txt index 495b266f2f..0267cf09b2 100644 --- a/examples/qml/referenceexamples/coercion/CMakeLists.txt +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/CMakeLists.txt @@ -4,15 +4,16 @@ cmake_minimum_required(VERSION 3.16) project(coercion LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/coercion") +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion") find_package(Qt6 REQUIRED COMPONENTS Core Qml) +qt_standard_project_setup() + +qt_policy(SET QTP0001 NEW) qt_add_executable(coercion birthdayparty.cpp birthdayparty.h @@ -32,8 +33,7 @@ target_link_libraries(coercion PUBLIC qt_add_qml_module(coercion URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH + QML_FILES Main.qml ) install(TARGETS coercion diff --git a/examples/qml/referenceexamples/coercion/example.qml b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/Main.qml index 99469bec57..1c3fe141ca 100644 --- a/examples/qml/referenceexamples/coercion/example.qml +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/Main.qml @@ -1,9 +1,8 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import People -// ![0] BirthdayParty { host: Boy { name: "Bob Jones" @@ -15,4 +14,3 @@ BirthdayParty { Girl { name: "Anne Brown" } ] } -// ![0] diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.cpp new file mode 100644 index 0000000000..ad38f284e7 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.cpp @@ -0,0 +1,99 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "birthdayparty.h" + +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(); +} diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.h b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.h new file mode 100644 index 0000000000..f9a5c126e3 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/birthdayparty.h @@ -0,0 +1,48 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include "person.h" + +#include <QObject> +#include <QQmlListProperty> + +class BirthdayParty : public QObject +{ + Q_OBJECT + Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged FINAL) + Q_PROPERTY(QQmlListProperty<Person> guests READ guests NOTIFY guestsChanged FINAL) + QML_ELEMENT +public: + using QObject::QObject; + + Person *host() const; + void setHost(Person *); + + QQmlListProperty<Person> guests(); + void appendGuest(Person *); + qsizetype guestCount() const; + Person *guest(qsizetype) const; + void clearGuests(); + void replaceGuest(qsizetype, Person *); + void removeLastGuest(); + +signals: + void hostChanged(); + void guestsChanged(); + +private: + static void appendGuest(QQmlListProperty<Person> *list, Person *); + static qsizetype guestCount(QQmlListProperty<Person> *); + static Person *guest(QQmlListProperty<Person> *, qsizetype); + static void clearGuests(QQmlListProperty<Person> *); + static void replaceGuest(QQmlListProperty<Person> *, qsizetype, Person *); + static void removeLastGuest(QQmlListProperty<Person> *); + + Person *m_host = nullptr; + QList<Person *> m_guests; +}; + +#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/coercion/coercion.pro b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/coercion.pro index 225fd13e08..1ba8194965 100644 --- a/examples/qml/referenceexamples/coercion/coercion.pro +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/coercion.pro @@ -10,6 +10,3 @@ SOURCES += main.cpp \ HEADERS += person.h \ birthdayparty.h RESOURCES += coercion.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/coercion -INSTALLS += target diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/coercion.qrc b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/coercion.qrc new file mode 100644 index 0000000000..b1eeb489e2 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/coercion.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/qt/qml/People/"> + <file>Main.qml</file> + <file alias="qmldir">qmldir.in</file> +</qresource> +</RCC> diff --git a/examples/qml/referenceexamples/coercion/main.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/main.cpp index 5bcd9681e3..fa26448f44 100644 --- a/examples/qml/referenceexamples/coercion/main.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/main.cpp @@ -1,20 +1,22 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QCoreApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> #include "birthdayparty.h" #include "person.h" -int main(int argc, char ** argv) +#include <QCoreApplication> +#include <QDebug> +#include <QQmlComponent> +#include <QQmlEngine> + +int main(int argc, char **argv) { QCoreApplication app(argc, argv); QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *party = qobject_cast<BirthdayParty *>(component.create()); + QQmlComponent component(&engine); + component.loadFromModule("People", "Main"); + std::unique_ptr<BirthdayParty> party{ qobject_cast<BirthdayParty *>(component.create()) }; if (party && party->host()) { qInfo() << party->host()->name() << "is having a birthday!"; diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/person.cpp new file mode 100644 index 0000000000..f8f4b1d2f4 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/person.cpp @@ -0,0 +1,30 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "person.h" + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &name) +{ + if (m_name != name) { + m_name = name; + emit nameChanged(); + } +} + +int Person::shoeSize() const +{ + return m_shoeSize; +} + +void Person::setShoeSize(int shoeSize) +{ + if (m_shoeSize != shoeSize) { + m_shoeSize = shoeSize; + emit shoeSizeChanged(); + } +} diff --git a/examples/qml/referenceexamples/coercion/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/person.h index ea9ff970b4..99fc9209ab 100644 --- a/examples/qml/referenceexamples/coercion/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/person.h @@ -1,21 +1,19 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef PERSON_H #define PERSON_H -#include <QObject> #include <QtQml/qqml.h> +#include <QObject> class Person : public QObject { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) - //![0] + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) + Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize NOTIFY shoeSizeChanged FINAL) QML_ELEMENT QML_UNCREATABLE("Person is an abstract base class.") - //![0] public: using QObject::QObject; @@ -25,12 +23,15 @@ public: int shoeSize() const; void setShoeSize(int); +signals: + void nameChanged(); + void shoeSizeChanged(); + private: QString m_name; int m_shoeSize = 0; }; -// ![1] class Boy : public Person { Q_OBJECT @@ -39,7 +40,6 @@ public: using Person::Person; }; -//! [girl class] class Girl : public Person { Q_OBJECT @@ -47,8 +47,5 @@ class Girl : public Person public: using Person::Person; }; -//! [girl class] - -// ![1] #endif // PERSON_H diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/qmldir.in b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/qmldir.in new file mode 100644 index 0000000000..3ccd68f7cc --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced2-Inheritance-and-coercion/qmldir.in @@ -0,0 +1,4 @@ +module People +typeinfo coercion.qmltypes +prefer :/qt/qml/People/ +Main 254.0 Main.qml diff --git a/examples/qml/referenceexamples/default/CMakeLists.txt b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/CMakeLists.txt index 681085f386..079d965280 100644 --- a/examples/qml/referenceexamples/default/CMakeLists.txt +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/CMakeLists.txt @@ -4,15 +4,16 @@ cmake_minimum_required(VERSION 3.16) project(default LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/default") +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/tutorials/extending-qml-advanced/advanced3-Default-properties") find_package(Qt6 REQUIRED COMPONENTS Core Qml) +qt_standard_project_setup() + +qt_policy(SET QTP0001 NEW) qt_add_executable(default birthdayparty.cpp birthdayparty.h @@ -32,8 +33,7 @@ target_link_libraries(default PUBLIC qt_add_qml_module(default URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH + QML_FILES Main.qml ) install(TARGETS default diff --git a/examples/qml/referenceexamples/default/example.qml b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/Main.qml index addc74dfb5..1070427cb0 100644 --- a/examples/qml/referenceexamples/default/example.qml +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/Main.qml @@ -1,9 +1,8 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import People -// ![0] BirthdayParty { host: Boy { name: "Bob Jones" @@ -14,4 +13,3 @@ BirthdayParty { Boy { name: "Jack Smith" } Girl { name: "Anne Brown" } } -// ![0] diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/birthdayparty.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/birthdayparty.cpp new file mode 100644 index 0000000000..ad38f284e7 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/birthdayparty.cpp @@ -0,0 +1,99 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "birthdayparty.h" + +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(); +} diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/birthdayparty.h b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/birthdayparty.h new file mode 100644 index 0000000000..4d7e61a487 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/birthdayparty.h @@ -0,0 +1,49 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include "person.h" + +#include <QObject> +#include <QQmlListProperty> + +class BirthdayParty : public QObject +{ + Q_OBJECT + Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged FINAL) + Q_PROPERTY(QQmlListProperty<Person> guests READ guests NOTIFY guestsChanged FINAL) + Q_CLASSINFO("DefaultProperty", "guests") + QML_ELEMENT +public: + using QObject::QObject; + + Person *host() const; + void setHost(Person *); + + QQmlListProperty<Person> guests(); + void appendGuest(Person *); + qsizetype guestCount() const; + Person *guest(qsizetype) const; + void clearGuests(); + void replaceGuest(qsizetype, Person *); + void removeLastGuest(); + +signals: + void hostChanged(); + void guestsChanged(); + +private: + static void appendGuest(QQmlListProperty<Person> *list, Person *); + static qsizetype guestCount(QQmlListProperty<Person> *); + static Person *guest(QQmlListProperty<Person> *, qsizetype); + static void clearGuests(QQmlListProperty<Person> *); + static void replaceGuest(QQmlListProperty<Person> *, qsizetype, Person *); + static void removeLastGuest(QQmlListProperty<Person> *); + + Person *m_host = nullptr; + QList<Person *> m_guests; +}; + +#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/default/default.pro b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/default.pro index f52f749ddd..65dd27213b 100644 --- a/examples/qml/referenceexamples/default/default.pro +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/default.pro @@ -10,6 +10,3 @@ SOURCES += main.cpp \ HEADERS += person.h \ birthdayparty.h RESOURCES += default.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/default -INSTALLS += target diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/default.qrc b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/default.qrc new file mode 100644 index 0000000000..b1eeb489e2 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/default.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/qt/qml/People/"> + <file>Main.qml</file> + <file alias="qmldir">qmldir.in</file> +</qresource> +</RCC> diff --git a/examples/qml/referenceexamples/default/main.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/main.cpp index 5e7e711ada..fa26448f44 100644 --- a/examples/qml/referenceexamples/default/main.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/main.cpp @@ -1,19 +1,22 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QCoreApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> + #include "birthdayparty.h" #include "person.h" -int main(int argc, char ** argv) +#include <QCoreApplication> +#include <QDebug> +#include <QQmlComponent> +#include <QQmlEngine> + +int main(int argc, char **argv) { QCoreApplication app(argc, argv); QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *party = qobject_cast<BirthdayParty *>(component.create()); + QQmlComponent component(&engine); + component.loadFromModule("People", "Main"); + std::unique_ptr<BirthdayParty> party{ qobject_cast<BirthdayParty *>(component.create()) }; if (party && party->host()) { qInfo() << party->host()->name() << "is having a birthday!"; diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/person.cpp new file mode 100644 index 0000000000..f8f4b1d2f4 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/person.cpp @@ -0,0 +1,30 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "person.h" + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &name) +{ + if (m_name != name) { + m_name = name; + emit nameChanged(); + } +} + +int Person::shoeSize() const +{ + return m_shoeSize; +} + +void Person::setShoeSize(int shoeSize) +{ + if (m_shoeSize != shoeSize) { + m_shoeSize = shoeSize; + emit shoeSizeChanged(); + } +} diff --git a/examples/qml/referenceexamples/default/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/person.h index 04ab53696d..99fc9209ab 100644 --- a/examples/qml/referenceexamples/default/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/person.h @@ -1,18 +1,19 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef PERSON_H #define PERSON_H -#include <QObject> #include <QtQml/qqml.h> +#include <QObject> class Person : public QObject { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) - QML_ANONYMOUS + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) + Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize NOTIFY shoeSizeChanged FINAL) + QML_ELEMENT + QML_UNCREATABLE("Person is an abstract base class.") public: using QObject::QObject; @@ -21,6 +22,11 @@ public: int shoeSize() const; void setShoeSize(int); + +signals: + void nameChanged(); + void shoeSizeChanged(); + private: QString m_name; int m_shoeSize = 0; diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/qmldir.in b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/qmldir.in new file mode 100644 index 0000000000..da1b995d64 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced3-Default-properties/qmldir.in @@ -0,0 +1,4 @@ +module People +typeinfo default.qmltypes +prefer :/qt/qml/People/ +Main 254.0 Main.qml diff --git a/examples/qml/referenceexamples/grouped/CMakeLists.txt b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/CMakeLists.txt index b1c7587b58..89aff6402f 100644 --- a/examples/qml/referenceexamples/grouped/CMakeLists.txt +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/CMakeLists.txt @@ -4,15 +4,16 @@ cmake_minimum_required(VERSION 3.16) project(grouped LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/grouped") +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties") + +find_package(Qt6 REQUIRED COMPONENTS Core Qml Gui) +qt_standard_project_setup() -find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml) +qt_policy(SET QTP0001 NEW) qt_add_executable(grouped birthdayparty.cpp birthdayparty.h @@ -27,14 +28,15 @@ set_target_properties(grouped PROPERTIES target_link_libraries(grouped PUBLIC Qt::Core - Qt::Gui Qt::Qml + Qt::Gui ) qt_add_qml_module(grouped URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH + QML_FILES Main.qml + DEPENDENCIES + QtQuick ) install(TARGETS grouped diff --git a/examples/qml/referenceexamples/grouped/example.qml b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/Main.qml index 31d122e647..27951b5ea8 100644 --- a/examples/qml/referenceexamples/grouped/example.qml +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/Main.qml @@ -1,10 +1,9 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import People import QtQuick // For QColor -// ![0] BirthdayParty { host: Boy { name: "Bob Jones" @@ -13,11 +12,8 @@ BirthdayParty { Boy { name: "Leo Hodges" -//![grouped] shoe { size: 10; color: "black"; brand: "Thebok"; price: 59.95 } -//![grouped] } - // ![1] Boy { name: "Jack Smith" shoe { @@ -27,15 +23,11 @@ BirthdayParty { price: 19.95 } } - // ![1] Girl { name: "Anne Brown" -//![ungrouped] shoe.size: 7 shoe.color: "red" shoe.brand: "Job Macobs" - shoe.price: 699.99 -//![ungrouped] + shoe.price: 99.99 } } -// ![0] diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/birthdayparty.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/birthdayparty.cpp new file mode 100644 index 0000000000..ad38f284e7 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/birthdayparty.cpp @@ -0,0 +1,99 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "birthdayparty.h" + +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(); +} diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/birthdayparty.h b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/birthdayparty.h new file mode 100644 index 0000000000..4d7e61a487 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/birthdayparty.h @@ -0,0 +1,49 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include "person.h" + +#include <QObject> +#include <QQmlListProperty> + +class BirthdayParty : public QObject +{ + Q_OBJECT + Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged FINAL) + Q_PROPERTY(QQmlListProperty<Person> guests READ guests NOTIFY guestsChanged FINAL) + Q_CLASSINFO("DefaultProperty", "guests") + QML_ELEMENT +public: + using QObject::QObject; + + Person *host() const; + void setHost(Person *); + + QQmlListProperty<Person> guests(); + void appendGuest(Person *); + qsizetype guestCount() const; + Person *guest(qsizetype) const; + void clearGuests(); + void replaceGuest(qsizetype, Person *); + void removeLastGuest(); + +signals: + void hostChanged(); + void guestsChanged(); + +private: + static void appendGuest(QQmlListProperty<Person> *list, Person *); + static qsizetype guestCount(QQmlListProperty<Person> *); + static Person *guest(QQmlListProperty<Person> *, qsizetype); + static void clearGuests(QQmlListProperty<Person> *); + static void replaceGuest(QQmlListProperty<Person> *, qsizetype, Person *); + static void removeLastGuest(QQmlListProperty<Person> *); + + Person *m_host = nullptr; + QList<Person *> m_guests; +}; + +#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/grouped/grouped.pro b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/grouped.pro index 1513ac552d..52e2937edf 100644 --- a/examples/qml/referenceexamples/grouped/grouped.pro +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/grouped.pro @@ -10,6 +10,3 @@ SOURCES += main.cpp \ HEADERS += person.h \ birthdayparty.h RESOURCES += grouped.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/grouped -INSTALLS += target diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/grouped.qrc b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/grouped.qrc new file mode 100644 index 0000000000..b1eeb489e2 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/grouped.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/qt/qml/People/"> + <file>Main.qml</file> + <file alias="qmldir">qmldir.in</file> +</qresource> +</RCC> diff --git a/examples/qml/referenceexamples/grouped/main.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/main.cpp index 16f17262a2..0721d496f0 100644 --- a/examples/qml/referenceexamples/grouped/main.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/main.cpp @@ -1,19 +1,22 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QCoreApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> + #include "birthdayparty.h" #include "person.h" -int main(int argc, char ** argv) +#include <QCoreApplication> +#include <QDebug> +#include <QQmlComponent> +#include <QQmlEngine> + +int main(int argc, char **argv) { QCoreApplication app(argc, argv); QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *party = qobject_cast<BirthdayParty *>(component.create()); + QQmlComponent component(&engine); + component.loadFromModule("People", "Main"); + std::unique_ptr<BirthdayParty> party{ qobject_cast<BirthdayParty *>(component.create()) }; if (party && party->host()) { qInfo() << party->host()->name() << "is having a birthday!"; diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.cpp new file mode 100644 index 0000000000..fe3d19b58d --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.cpp @@ -0,0 +1,87 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "person.h" + +Person::Person(QObject *parent) : QObject(parent) +{ + m_shoe = new ShoeDescription(this); +} + +int ShoeDescription::size() const +{ + return m_size; +} + +void ShoeDescription::setSize(int size) +{ + if (m_size != size) { + m_size = size; + emit shoeChanged(); + } +} + +QColor ShoeDescription::color() const +{ + return m_color; +} + +void ShoeDescription::setColor(const QColor &color) +{ + if (m_color != color) { + m_color = color; + emit shoeChanged(); + } +} + +QString ShoeDescription::brand() const +{ + return m_brand; +} + +void ShoeDescription::setBrand(const QString &brand) +{ + if (m_brand != brand) { + m_brand = brand; + emit shoeChanged(); + } +} + +qreal ShoeDescription::price() const +{ + return m_price; +} + +void ShoeDescription::setPrice(qreal price) +{ + if (m_price != price) { + m_price = price; + emit shoeChanged(); + } +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &name) +{ + if (m_name != name) { + m_name = name; + emit nameChanged(); + } +} + +ShoeDescription *Person::shoe() const +{ + return m_shoe; +} + +void Person::setShoe(ShoeDescription *shoe) +{ + if (m_shoe != shoe) { + m_shoe = shoe; + emit shoeChanged(); + } +} diff --git a/examples/qml/referenceexamples/binding/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.h index 8314348bdf..ecb4545097 100644 --- a/examples/qml/referenceexamples/binding/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.h @@ -1,20 +1,20 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef PERSON_H #define PERSON_H -#include <QObject> -#include <QColor> #include <QtQml/qqml.h> +#include <QColor> +#include <QObject> class ShoeDescription : public QObject { Q_OBJECT - Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged) - Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY shoeChanged) - Q_PROPERTY(QString brand READ brand WRITE setBrand NOTIFY shoeChanged) - Q_PROPERTY(qreal price READ price WRITE setPrice NOTIFY shoeChanged) + Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged FINAL) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY shoeChanged FINAL) + Q_PROPERTY(QString brand READ brand WRITE setBrand NOTIFY shoeChanged FINAL) + Q_PROPERTY(qreal price READ price WRITE setPrice NOTIFY shoeChanged FINAL) QML_ANONYMOUS public: using QObject::QObject; @@ -44,25 +44,28 @@ private: class Person : public QObject { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) -// ![0] - Q_PROPERTY(ShoeDescription *shoe READ shoe CONSTANT) -// ![0] - QML_ANONYMOUS + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) + Q_PROPERTY(ShoeDescription *shoe READ shoe WRITE setShoe NOTIFY shoeChanged FINAL) + QML_ELEMENT + QML_UNCREATABLE("Person is an abstract base class.") public: using QObject::QObject; + Person(QObject *parent = nullptr); + QString name() const; void setName(const QString &); - ShoeDescription *shoe(); + ShoeDescription *shoe() const; + void setShoe(ShoeDescription *shoe); signals: void nameChanged(); + void shoeChanged(); private: QString m_name; - ShoeDescription m_shoe; + ShoeDescription *m_shoe = nullptr; }; class Boy : public Person diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/qmldir.in b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/qmldir.in new file mode 100644 index 0000000000..2e634e41af --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/qmldir.in @@ -0,0 +1,5 @@ +module People +typeinfo grouped.qmltypes +prefer :/qt/qml/People/ +Main 254.0 Main.qml +depends QtQuick diff --git a/examples/qml/referenceexamples/attached/CMakeLists.txt b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/CMakeLists.txt index ce4ff3e80f..ca062f4675 100644 --- a/examples/qml/referenceexamples/attached/CMakeLists.txt +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/CMakeLists.txt @@ -4,15 +4,16 @@ cmake_minimum_required(VERSION 3.16) project(attached LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/attached") +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties") + +find_package(Qt6 REQUIRED COMPONENTS Core Qml Quick) +qt_standard_project_setup() -find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml) +qt_policy(SET QTP0001 NEW) qt_add_executable(attached birthdayparty.cpp birthdayparty.h @@ -27,14 +28,15 @@ set_target_properties(attached PROPERTIES target_link_libraries(attached PUBLIC Qt::Core - Qt::Gui Qt::Qml + Qt::Quick ) qt_add_qml_module(attached URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH + QML_FILES Main.qml + DEPENDENCIES + QtQuick ) install(TARGETS attached diff --git a/examples/qml/referenceexamples/attached/example.qml b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/Main.qml index a4e675fe51..8175eae209 100644 --- a/examples/qml/referenceexamples/attached/example.qml +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/Main.qml @@ -1,32 +1,23 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import People import QtQuick // For QColor -//! [begin] BirthdayParty { -//! [begin] - -//! [rsvp] Boy { name: "Robert Campbell" - BirthdayParty.rsvp: "2009-07-01" + BirthdayParty.rsvp: Date.fromLocaleString(Qt.locale(), "2023-03-01", "yyyy-MM-dd") } -//! [rsvp] - // ![1] + Boy { name: "Leo Hodges" shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } - - BirthdayParty.rsvp: "2009-07-06" + BirthdayParty.rsvp: Date.fromLocaleString(Qt.locale(), "2023-03-03", "yyyy-MM-dd") } - // ![1] + host: Boy { name: "Jack Smith" shoe { size: 8; color: "blue"; brand: "Puma"; price: 19.95 } } -//! [end] } -//! [end] - diff --git a/examples/qml/referenceexamples/attached/attached.pro b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/attached.pro index 8c66b189f7..ab154eb48b 100644 --- a/examples/qml/referenceexamples/attached/attached.pro +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/attached.pro @@ -1,6 +1,6 @@ -QT += qml -CONFIG += qmltypes +QT += qml quick +CONFIG += qmltypes QML_IMPORT_NAME = People QML_IMPORT_MAJOR_VERSION = 1 @@ -10,6 +10,3 @@ SOURCES += main.cpp \ HEADERS += person.h \ birthdayparty.h RESOURCES += attached.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/attached -INSTALLS += target diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/attached.qrc b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/attached.qrc new file mode 100644 index 0000000000..b1eeb489e2 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/attached.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/qt/qml/People/"> + <file>Main.qml</file> + <file alias="qmldir">qmldir.in</file> +</qresource> +</RCC> 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); +} diff --git a/examples/qml/referenceexamples/binding/birthdayparty.h b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/birthdayparty.h index 3b4c92cae3..1b2503895a 100644 --- a/examples/qml/referenceexamples/binding/birthdayparty.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/birthdayparty.h @@ -1,19 +1,19 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H -#include <QObject> +#include "person.h" + #include <QDate> -#include <QDebug> +#include <QObject> #include <qqml.h> -#include "person.h" class BirthdayPartyAttached : public QObject { Q_OBJECT - Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp NOTIFY rsvpChanged) + Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp NOTIFY rsvpChanged FINAL) QML_ANONYMOUS public: using QObject::QObject; @@ -31,14 +31,12 @@ private: class BirthdayParty : public QObject { Q_OBJECT -// ![0] - Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged) -// ![0] - Q_PROPERTY(QQmlListProperty<Person> guests READ guests) - Q_PROPERTY(QString announcement READ announcement WRITE setAnnouncement) + Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged FINAL) + Q_PROPERTY(QQmlListProperty<Person> guests READ guests NOTIFY guestsChanged FINAL) Q_CLASSINFO("DefaultProperty", "guests") QML_ELEMENT QML_ATTACHED(BirthdayPartyAttached) + public: using QObject::QObject; @@ -46,20 +44,27 @@ public: void setHost(Person *); QQmlListProperty<Person> guests(); + void appendGuest(Person *); qsizetype guestCount() const; Person *guest(qsizetype) const; - - QString announcement() const; - void setAnnouncement(const QString &); + void clearGuests(); + void replaceGuest(qsizetype, Person *); + void removeLastGuest(); static BirthdayPartyAttached *qmlAttachedProperties(QObject *); - void startParty(); signals: - void partyStarted(QTime time); void hostChanged(); + void guestsChanged(); private: + static void appendGuest(QQmlListProperty<Person> *list, Person *); + static qsizetype guestCount(QQmlListProperty<Person> *); + static Person *guest(QQmlListProperty<Person> *, qsizetype); + static void clearGuests(QQmlListProperty<Person> *); + static void replaceGuest(QQmlListProperty<Person> *, qsizetype, Person *); + static void removeLastGuest(QQmlListProperty<Person> *); + Person *m_host = nullptr; QList<Person *> m_guests; }; diff --git a/examples/qml/referenceexamples/attached/main.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/main.cpp index cfe0a1b9b2..09691f3b6a 100644 --- a/examples/qml/referenceexamples/attached/main.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/main.cpp @@ -1,20 +1,22 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QCoreApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> #include "birthdayparty.h" #include "person.h" -int main(int argc, char ** argv) +#include <QCoreApplication> +#include <QDebug> +#include <QQmlComponent> +#include <QQmlEngine> + +int main(int argc, char **argv) { QCoreApplication app(argc, argv); QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *party = qobject_cast<BirthdayParty *>(component.create()); + QQmlComponent component(&engine); + component.loadFromModule("People", "Main"); + std::unique_ptr<BirthdayParty> party{ qobject_cast<BirthdayParty *>(component.create()) }; if (party && party->host()) { qInfo() << party->host()->name() << "is having a birthday!"; @@ -27,13 +29,11 @@ int main(int argc, char ** argv) for (qsizetype ii = 0; ii < party->guestCount(); ++ii) { Person *guest = party->guest(ii); - //! [query rsvp] QDate rsvpDate; QObject *attached = qmlAttachedPropertiesObject<BirthdayParty>(guest, false); if (attached) rsvpDate = attached->property("rsvp").toDate(); - //! [query rsvp] if (rsvpDate.isNull()) qInfo() << " " << guest->name() << "RSVP date: Hasn't RSVP'd"; else diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.cpp new file mode 100644 index 0000000000..fe3d19b58d --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.cpp @@ -0,0 +1,87 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "person.h" + +Person::Person(QObject *parent) : QObject(parent) +{ + m_shoe = new ShoeDescription(this); +} + +int ShoeDescription::size() const +{ + return m_size; +} + +void ShoeDescription::setSize(int size) +{ + if (m_size != size) { + m_size = size; + emit shoeChanged(); + } +} + +QColor ShoeDescription::color() const +{ + return m_color; +} + +void ShoeDescription::setColor(const QColor &color) +{ + if (m_color != color) { + m_color = color; + emit shoeChanged(); + } +} + +QString ShoeDescription::brand() const +{ + return m_brand; +} + +void ShoeDescription::setBrand(const QString &brand) +{ + if (m_brand != brand) { + m_brand = brand; + emit shoeChanged(); + } +} + +qreal ShoeDescription::price() const +{ + return m_price; +} + +void ShoeDescription::setPrice(qreal price) +{ + if (m_price != price) { + m_price = price; + emit shoeChanged(); + } +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &name) +{ + if (m_name != name) { + m_name = name; + emit nameChanged(); + } +} + +ShoeDescription *Person::shoe() const +{ + return m_shoe; +} + +void Person::setShoe(ShoeDescription *shoe) +{ + if (m_shoe != shoe) { + m_shoe = shoe; + emit shoeChanged(); + } +} diff --git a/examples/qml/referenceexamples/attached/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.h index 9c617ee9ab..ecb4545097 100644 --- a/examples/qml/referenceexamples/attached/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.h @@ -1,20 +1,20 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef PERSON_H #define PERSON_H -#include <QObject> -#include <QColor> #include <QtQml/qqml.h> +#include <QColor> +#include <QObject> class ShoeDescription : public QObject { Q_OBJECT - Q_PROPERTY(int size READ size WRITE setSize) - Q_PROPERTY(QColor color READ color WRITE setColor) - Q_PROPERTY(QString brand READ brand WRITE setBrand) - Q_PROPERTY(qreal price READ price WRITE setPrice) + Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged FINAL) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY shoeChanged FINAL) + Q_PROPERTY(QString brand READ brand WRITE setBrand NOTIFY shoeChanged FINAL) + Q_PROPERTY(qreal price READ price WRITE setPrice NOTIFY shoeChanged FINAL) QML_ANONYMOUS public: using QObject::QObject; @@ -31,6 +31,9 @@ public: qreal price() const; void setPrice(qreal); +signals: + void shoeChanged(); + private: int m_size = 0; QColor m_color; @@ -41,19 +44,28 @@ private: class Person : public QObject { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(ShoeDescription *shoe READ shoe) - QML_ANONYMOUS + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) + Q_PROPERTY(ShoeDescription *shoe READ shoe WRITE setShoe NOTIFY shoeChanged FINAL) + QML_ELEMENT + QML_UNCREATABLE("Person is an abstract base class.") public: using QObject::QObject; + Person(QObject *parent = nullptr); + QString name() const; void setName(const QString &); - ShoeDescription *shoe(); + ShoeDescription *shoe() const; + void setShoe(ShoeDescription *shoe); + +signals: + void nameChanged(); + void shoeChanged(); + private: QString m_name; - ShoeDescription m_shoe; + ShoeDescription *m_shoe = nullptr; }; class Boy : public Person diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/qmldir.in b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/qmldir.in new file mode 100644 index 0000000000..1038298c01 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/qmldir.in @@ -0,0 +1,5 @@ +module People +typeinfo attached.qmltypes +prefer :/qt/qml/People/ +Main 254.0 Main.qml +depends QtQuick diff --git a/examples/qml/referenceexamples/valuesource/CMakeLists.txt b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/CMakeLists.txt index 266bb2b754..67ca56153c 100644 --- a/examples/qml/referenceexamples/valuesource/CMakeLists.txt +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/CMakeLists.txt @@ -4,15 +4,16 @@ cmake_minimum_required(VERSION 3.16) project(valuesource LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/referenceexamples/valuesource") +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source") + +find_package(Qt6 REQUIRED COMPONENTS Core Qml Quick) +qt_standard_project_setup() -find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml) +qt_policy(SET QTP0001 NEW) qt_add_executable(valuesource birthdayparty.cpp birthdayparty.h @@ -28,14 +29,15 @@ set_target_properties(valuesource PROPERTIES target_link_libraries(valuesource PUBLIC Qt::Core - Qt::Gui Qt::Qml + Qt::Quick ) qt_add_qml_module(valuesource URI People - QML_FILES example.qml - NO_RESOURCE_TARGET_PATH + QML_FILES Main.qml + DEPENDENCIES + QtQuick ) install(TARGETS valuesource diff --git a/examples/qml/referenceexamples/valuesource/example.qml b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/Main.qml index b6efe211c1..7f79d0a0c4 100644 --- a/examples/qml/referenceexamples/valuesource/example.qml +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/Main.qml @@ -1,13 +1,14 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import People import QtQuick // For QColor -// ![0] BirthdayParty { - HappyBirthdaySong on announcement { name: "Bob Jones" } -// ![0] + id: party + HappyBirthdaySong on announcement { + name: party.host.name + } onPartyStarted: (time) => { console.log("This party started rockin' at " + time); } @@ -19,7 +20,7 @@ BirthdayParty { Boy { name: "Leo Hodges" - BirthdayParty.rsvp: "2009-07-06" + BirthdayParty.rsvp: Date.fromLocaleString(Qt.locale(), "2023-03-01", "yyyy-MM-dd") shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } } Boy { @@ -28,13 +29,10 @@ BirthdayParty { } Girl { name: "Anne Brown" - BirthdayParty.rsvp: "2009-07-01" + BirthdayParty.rsvp: Date.fromLocaleString(Qt.locale(), "2023-03-03", "yyyy-MM-dd") shoe.size: 7 shoe.color: "red" shoe.brand: "Marc Jacobs" - shoe.price: 699.99 + shoe.price: 99.99 } - -// ![1] } -// ![1] diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/birthdayparty.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/birthdayparty.cpp new file mode 100644 index 0000000000..b14f7ef315 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/birthdayparty.cpp @@ -0,0 +1,137 @@ +// 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(); +} + +void BirthdayParty::startParty() +{ + QDateTime time = QDateTime::currentDateTime(); + emit partyStarted(time); +} + +QString BirthdayParty::announcement() const +{ + return m_announcement; +} + +void BirthdayParty::setAnnouncement(const QString &announcement) +{ + if (m_announcement != announcement) { + m_announcement = announcement; + emit announcementChanged(); + } + qInfo().noquote() << announcement; +} + +BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) +{ + return new BirthdayPartyAttached(object); +} diff --git a/examples/qml/referenceexamples/valuesource/birthdayparty.h b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/birthdayparty.h index 922dac838b..799a3fa969 100644 --- a/examples/qml/referenceexamples/valuesource/birthdayparty.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/birthdayparty.h @@ -1,19 +1,21 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H -#include <QObject> +#include "person.h" + #include <QDate> #include <QDebug> +#include <QObject> +#include <QQmlListProperty> #include <qqml.h> -#include "person.h" class BirthdayPartyAttached : public QObject { Q_OBJECT - Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) + Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp NOTIFY rsvpChanged FINAL) QML_ANONYMOUS public: using QObject::QObject; @@ -21,6 +23,9 @@ public: QDate rsvp() const; void setRsvp(QDate); +signals: + void rsvpChanged(); + private: QDate m_rsvp; }; @@ -28,11 +33,9 @@ private: class BirthdayParty : public QObject { Q_OBJECT - Q_PROPERTY(Person *host READ host WRITE setHost) - Q_PROPERTY(QQmlListProperty<Person> guests READ guests) -// ![0] - Q_PROPERTY(QString announcement READ announcement WRITE setAnnouncement) -// ![0] + Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged FINAL) + Q_PROPERTY(QQmlListProperty<Person> guests READ guests NOTIFY guestsChanged FINAL) + Q_PROPERTY(QString announcement READ announcement WRITE setAnnouncement NOTIFY announcementChanged FINAL) Q_CLASSINFO("DefaultProperty", "guests") QML_ELEMENT QML_ATTACHED(BirthdayPartyAttached) @@ -42,23 +45,38 @@ public: Person *host() const; void setHost(Person *); + QString announcement() const; + void setAnnouncement(const QString &); + QQmlListProperty<Person> guests(); + void appendGuest(Person *); qsizetype guestCount() const; Person *guest(qsizetype) const; - - QString announcement() const; - void setAnnouncement(const QString &); + void clearGuests(); + void replaceGuest(qsizetype, Person *); + void removeLastGuest(); static BirthdayPartyAttached *qmlAttachedProperties(QObject *); void startParty(); signals: - void partyStarted(QTime time); + void hostChanged(); + void guestsChanged(); + void partyStarted(QDateTime time); + void announcementChanged(); private: + static void appendGuest(QQmlListProperty<Person> *, Person *); + static qsizetype guestCount(QQmlListProperty<Person> *); + static Person *guest(QQmlListProperty<Person> *, qsizetype); + static void clearGuests(QQmlListProperty<Person> *); + static void replaceGuest(QQmlListProperty<Person> *, qsizetype, Person *); + static void removeLastGuest(QQmlListProperty<Person> *); + Person *m_host = nullptr; QList<Person *> m_guests; + QString m_announcement; }; #endif // BIRTHDAYPARTY_H diff --git a/examples/qml/referenceexamples/binding/happybirthdaysong.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/happybirthdaysong.cpp index cf71a7f213..7a756a4a71 100644 --- a/examples/qml/referenceexamples/binding/happybirthdaysong.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/happybirthdaysong.cpp @@ -1,19 +1,20 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + #include "happybirthdaysong.h" + #include <QTimer> -HappyBirthdaySong::HappyBirthdaySong(QObject *parent) : - QObject(parent) +HappyBirthdaySong::HappyBirthdaySong(QObject *parent) : QObject(parent) { auto *timer = new QTimer(this); QObject::connect(timer, &QTimer::timeout, this, &HappyBirthdaySong::advance); timer->start(1000); } -void HappyBirthdaySong::setTarget(const QQmlProperty &p) +void HappyBirthdaySong::setTarget(const QQmlProperty &target) { - m_target = p; + m_target = target; } QString HappyBirthdaySong::name() const @@ -23,10 +24,10 @@ QString HappyBirthdaySong::name() const void HappyBirthdaySong::setName(const QString &name) { - if (m_name == name) - return; - - m_name = name; + if (m_name != name) { + m_name = name; + emit nameChanged(); + } m_lyrics.clear(); m_lyrics << "Happy birthday to you,"; @@ -34,8 +35,6 @@ void HappyBirthdaySong::setName(const QString &name) m_lyrics << "Happy birthday dear " + m_name + ","; m_lyrics << "Happy birthday to you!"; m_lyrics << ""; - - emit nameChanged(); } void HappyBirthdaySong::advance() @@ -44,4 +43,3 @@ void HappyBirthdaySong::advance() m_target.write(m_lyrics.at(m_line)); } - diff --git a/examples/qml/referenceexamples/binding/happybirthdaysong.h b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/happybirthdaysong.h index 8705abfa83..13907d5485 100644 --- a/examples/qml/referenceexamples/binding/happybirthdaysong.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/happybirthdaysong.h @@ -1,20 +1,19 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + #ifndef HAPPYBIRTHDAYSONG_H #define HAPPYBIRTHDAYSONG_H -#include <QQmlPropertyValueSource> #include <QQmlProperty> +#include <QQmlPropertyValueSource> #include <qqml.h> - #include <QStringList> -#include <qqml.h> class HappyBirthdaySong : public QObject, public QQmlPropertyValueSource { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_INTERFACES(QQmlPropertyValueSource) + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) QML_ELEMENT public: explicit HappyBirthdaySong(QObject *parent = nullptr); @@ -24,11 +23,12 @@ public: QString name() const; void setName(const QString &); +signals: + void nameChanged(); + private slots: void advance(); -signals: - void nameChanged(); private: qsizetype m_line = -1; QStringList m_lyrics; @@ -37,4 +37,3 @@ private: }; #endif // HAPPYBIRTHDAYSONG_H - diff --git a/examples/qml/referenceexamples/valuesource/main.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/main.cpp index 716deeccda..6d67b6179e 100644 --- a/examples/qml/referenceexamples/valuesource/main.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/main.cpp @@ -1,20 +1,22 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QCoreApplication> -#include <QQmlEngine> -#include <QQmlComponent> -#include <QDebug> + #include "birthdayparty.h" -#include "happybirthdaysong.h" #include "person.h" -int main(int argc, char ** argv) +#include <QCoreApplication> +#include <QDebug> +#include <QQmlComponent> +#include <QQmlEngine> + +int main(int argc, char **argv) { QCoreApplication app(argc, argv); QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *party = qobject_cast<BirthdayParty *>(component.create()); + QQmlComponent component(&engine); + component.loadFromModule("People", "Main"); + std::unique_ptr<BirthdayParty> party{ qobject_cast<BirthdayParty *>(component.create()) }; if (party && party->host()) { qInfo() << party->host()->name() << "is having a birthday!"; @@ -28,8 +30,7 @@ int main(int argc, char ** argv) Person *guest = party->guest(ii); QDate rsvpDate; - QObject *attached = - qmlAttachedPropertiesObject<BirthdayParty>(guest, false); + QObject *attached = qmlAttachedPropertiesObject<BirthdayParty>(guest, false); if (attached) rsvpDate = attached->property("rsvp").toDate(); diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.cpp new file mode 100644 index 0000000000..fe3d19b58d --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.cpp @@ -0,0 +1,87 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "person.h" + +Person::Person(QObject *parent) : QObject(parent) +{ + m_shoe = new ShoeDescription(this); +} + +int ShoeDescription::size() const +{ + return m_size; +} + +void ShoeDescription::setSize(int size) +{ + if (m_size != size) { + m_size = size; + emit shoeChanged(); + } +} + +QColor ShoeDescription::color() const +{ + return m_color; +} + +void ShoeDescription::setColor(const QColor &color) +{ + if (m_color != color) { + m_color = color; + emit shoeChanged(); + } +} + +QString ShoeDescription::brand() const +{ + return m_brand; +} + +void ShoeDescription::setBrand(const QString &brand) +{ + if (m_brand != brand) { + m_brand = brand; + emit shoeChanged(); + } +} + +qreal ShoeDescription::price() const +{ + return m_price; +} + +void ShoeDescription::setPrice(qreal price) +{ + if (m_price != price) { + m_price = price; + emit shoeChanged(); + } +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &name) +{ + if (m_name != name) { + m_name = name; + emit nameChanged(); + } +} + +ShoeDescription *Person::shoe() const +{ + return m_shoe; +} + +void Person::setShoe(ShoeDescription *shoe) +{ + if (m_shoe != shoe) { + m_shoe = shoe; + emit shoeChanged(); + } +} diff --git a/examples/qml/referenceexamples/signal/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.h index 9c617ee9ab..ecb4545097 100644 --- a/examples/qml/referenceexamples/signal/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.h @@ -1,20 +1,20 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef PERSON_H #define PERSON_H -#include <QObject> -#include <QColor> #include <QtQml/qqml.h> +#include <QColor> +#include <QObject> class ShoeDescription : public QObject { Q_OBJECT - Q_PROPERTY(int size READ size WRITE setSize) - Q_PROPERTY(QColor color READ color WRITE setColor) - Q_PROPERTY(QString brand READ brand WRITE setBrand) - Q_PROPERTY(qreal price READ price WRITE setPrice) + Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged FINAL) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY shoeChanged FINAL) + Q_PROPERTY(QString brand READ brand WRITE setBrand NOTIFY shoeChanged FINAL) + Q_PROPERTY(qreal price READ price WRITE setPrice NOTIFY shoeChanged FINAL) QML_ANONYMOUS public: using QObject::QObject; @@ -31,6 +31,9 @@ public: qreal price() const; void setPrice(qreal); +signals: + void shoeChanged(); + private: int m_size = 0; QColor m_color; @@ -41,19 +44,28 @@ private: class Person : public QObject { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(ShoeDescription *shoe READ shoe) - QML_ANONYMOUS + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) + Q_PROPERTY(ShoeDescription *shoe READ shoe WRITE setShoe NOTIFY shoeChanged FINAL) + QML_ELEMENT + QML_UNCREATABLE("Person is an abstract base class.") public: using QObject::QObject; + Person(QObject *parent = nullptr); + QString name() const; void setName(const QString &); - ShoeDescription *shoe(); + ShoeDescription *shoe() const; + void setShoe(ShoeDescription *shoe); + +signals: + void nameChanged(); + void shoeChanged(); + private: QString m_name; - ShoeDescription m_shoe; + ShoeDescription *m_shoe = nullptr; }; class Boy : public Person diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/qmldir.in b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/qmldir.in new file mode 100644 index 0000000000..4c63c729d6 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/qmldir.in @@ -0,0 +1,5 @@ +module People +typeinfo valuesource.qmltypes +prefer :/qt/qml/People/ +Main 254.0 Main.qml +depends QtQuick diff --git a/examples/qml/referenceexamples/valuesource/valuesource.pro b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/valuesource.pro index 6d29cf1b70..c55299cecf 100644 --- a/examples/qml/referenceexamples/valuesource/valuesource.pro +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/valuesource.pro @@ -1,4 +1,4 @@ -QT += qml +QT += qml quick CONFIG += qmltypes QML_IMPORT_NAME = People @@ -12,6 +12,3 @@ HEADERS += person.h \ birthdayparty.h \ happybirthdaysong.h RESOURCES += valuesource.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/qml/referenceexamples/valuesource -INSTALLS += target diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/valuesource.qrc b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/valuesource.qrc new file mode 100644 index 0000000000..b1eeb489e2 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/valuesource.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/qt/qml/People/"> + <file>Main.qml</file> + <file alias="qmldir">qmldir.in</file> +</qresource> +</RCC> diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/CMakeLists.txt b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/CMakeLists.txt new file mode 100644 index 0000000000..3769b887c0 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/CMakeLists.txt @@ -0,0 +1,60 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) +project(foreign LANGUAGES CXX) + +if(NOT DEFINED INSTALL_EXAMPLESDIR) + set(INSTALL_EXAMPLESDIR "examples") +endif() + +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/tutorials/extending-qml-advanced/advanced7-Extension-objects") + +find_package(Qt6 REQUIRED COMPONENTS Core Qml Quick Gui) +qt_standard_project_setup() + +qt_policy(SET QTP0001 NEW) + +add_subdirectory(library/) + +qt_add_executable(foreign + birthdayparty.cpp + birthdayparty.h + foreigndisplay.h + happybirthdaysong.cpp + happybirthdaysong.h + person.cpp + person.h + main.cpp +) + +target_link_libraries(foreign PUBLIC + Qt::Core + Qt::Qml + Qt::Gui + library +) + +set_target_properties(foreign PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) + +target_include_directories(foreign PUBLIC + "${PROJECT_BINARY_DIR}" + "${PROJECT_SOURCE_DIR}/library" +) + +qt_add_qml_module(foreign + URI People + QML_FILES Main.qml + SOURCES foreigndisplay.h + DEPENDENCIES + QtQuick +) + +install(TARGETS foreign + RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" + LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" +) diff --git a/examples/qml/referenceexamples/binding/example.qml b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/Main.qml index 262d667903..988bea49f8 100644 --- a/examples/qml/referenceexamples/binding/example.qml +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/Main.qml @@ -1,26 +1,31 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import People import QtQuick // For QColor -// ![0] BirthdayParty { - id: theParty + id: party + HappyBirthdaySong on announcement { + name: party.host.name + } + + display: ThirdPartyDisplay { + foregroundColor: "black" + backgroundColor: "white" + } + + onPartyStarted: (time) => { console.log("This party started rockin' at " + time); } - HappyBirthdaySong on announcement { name: theParty.host.name } host: Boy { name: "Bob Jones" shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 } } -// ![0] - onPartyStarted: (time) => { console.log("This party started rockin' at " + time); } - Boy { name: "Leo Hodges" - BirthdayParty.rsvp: "2009-07-06" + BirthdayParty.rsvp: Date.fromLocaleString(Qt.locale(), "2023-03-01", "yyyy-MM-dd") shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 } } Boy { @@ -29,13 +34,10 @@ BirthdayParty { } Girl { name: "Anne Brown" - BirthdayParty.rsvp: "2009-07-01" + BirthdayParty.rsvp: Date.fromLocaleString(Qt.locale(), "2023-03-03", "yyyy-MM-dd") shoe.size: 7 shoe.color: "red" shoe.brand: "Marc Jacobs" - shoe.price: 699.99 + shoe.price: 99.99 } - -// ![1] } -// ![1] diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/application.pro b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/application.pro new file mode 100644 index 0000000000..b53397d49c --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/application.pro @@ -0,0 +1,26 @@ +TEMPLATE = app + +CONFIG += console + +QT += core qml + +DEPENDPATH += library +INCLUDEPATH += library +LIBS += -Llibrary/ -llibrary + +SOURCES += \ + birthdayparty.cpp \ + happybirthdaysong.cpp \ + main.cpp \ + person.cpp +HEADERS += \ + birthdayparty.h \ + foreigndisplay.h \ + happybirthdaysong.h \ + person.h + +CONFIG += qmltypes +QML_IMPORT_NAME = People +QML_IMPORT_MAJOR_VERSION = 1 + +RESOURCES += foreign.qrc diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/birthdayparty.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/birthdayparty.cpp new file mode 100644 index 0000000000..7a9debe195 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/birthdayparty.cpp @@ -0,0 +1,150 @@ +// 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(); +} + +void BirthdayParty::startParty() +{ + QDateTime time = QDateTime::currentDateTime(); + emit partyStarted(time); +} + +QString BirthdayParty::announcement() const +{ + return m_announcement; +} + +void BirthdayParty::setAnnouncement(const QString &announcement) +{ + if (m_announcement != announcement) { + m_announcement = announcement; + emit announcementChanged(); + } + m_display->setContent(announcement); +} + +ThirdPartyDisplay *BirthdayParty::display() const +{ + return m_display; +} + +void BirthdayParty::setDisplay(ThirdPartyDisplay *display) +{ + if (m_display != display) { + m_display = display; + emit displayChanged(); + } +} + +BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) +{ + return new BirthdayPartyAttached(object); +} diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/birthdayparty.h b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/birthdayparty.h new file mode 100644 index 0000000000..59c53f2484 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/birthdayparty.h @@ -0,0 +1,90 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef BIRTHDAYPARTY_H +#define BIRTHDAYPARTY_H + +#include "person.h" +#include "ThirdPartyDisplay.h" + +#include <QDate> +#include <QDebug> +#include <QObject> +#include <qqml.h> + +#include <memory> + +class BirthdayPartyAttached : public QObject +{ + Q_OBJECT + Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp NOTIFY rsvpChanged FINAL) + QML_ANONYMOUS +public: + using QObject::QObject; + + QDate rsvp() const; + void setRsvp(QDate); + +signals: + void rsvpChanged(); + +private: + QDate m_rsvp; +}; + +class BirthdayParty : public QObject +{ + Q_OBJECT + Q_PROPERTY(Person *host READ host WRITE setHost NOTIFY hostChanged FINAL) + Q_PROPERTY(QQmlListProperty<Person> guests READ guests NOTIFY guestsChanged FINAL) + Q_PROPERTY(QString announcement READ announcement WRITE setAnnouncement NOTIFY announcementChanged FINAL) + Q_PROPERTY(ThirdPartyDisplay *display READ display WRITE setDisplay NOTIFY displayChanged FINAL) + Q_CLASSINFO("DefaultProperty", "guests") + QML_ELEMENT + QML_ATTACHED(BirthdayPartyAttached) +public: + using QObject::QObject; + + Person *host() const; + void setHost(Person *); + + QQmlListProperty<Person> guests(); + void appendGuest(Person *); + qsizetype guestCount() const; + Person *guest(qsizetype) const; + void clearGuests(); + void replaceGuest(qsizetype, Person *); + void removeLastGuest(); + + QString announcement() const; + void setAnnouncement(const QString &); + + ThirdPartyDisplay *display() const; + void setDisplay(ThirdPartyDisplay *); + + static BirthdayPartyAttached *qmlAttachedProperties(QObject *); + + void startParty(); + +signals: + void hostChanged(); + void guestsChanged(); + void partyStarted(QDateTime time); + void announcementChanged(); + void displayChanged(); + +private: + static void appendGuest(QQmlListProperty<Person> *, Person *); + static qsizetype guestCount(QQmlListProperty<Person> *); + static Person *guest(QQmlListProperty<Person> *, qsizetype); + static void clearGuests(QQmlListProperty<Person> *); + static void replaceGuest(QQmlListProperty<Person> *, qsizetype, Person *); + static void removeLastGuest(QQmlListProperty<Person> *); + + Person *m_host = nullptr; + QList<Person *> m_guests; + QString m_announcement; + ThirdPartyDisplay *m_display = nullptr; +}; + +#endif // BIRTHDAYPARTY_H diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/foreign.pro b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/foreign.pro new file mode 100644 index 0000000000..b637cb0840 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/foreign.pro @@ -0,0 +1,7 @@ +TEMPLATE = subdirs + +SUBDIRS = \ + application.pro \ + library + +application.depends = library diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/foreign.qrc b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/foreign.qrc new file mode 100644 index 0000000000..b1eeb489e2 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/foreign.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/qt/qml/People/"> + <file>Main.qml</file> + <file alias="qmldir">qmldir.in</file> +</qresource> +</RCC> diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/foreigndisplay.h b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/foreigndisplay.h new file mode 100644 index 0000000000..ee42ca965c --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/foreigndisplay.h @@ -0,0 +1,20 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef FOREIGNDISPLAY_H +#define FOREIGNDISPLAY_H + +#include "ThirdPartyDisplay.h" + +#include <QColor> +#include <QObject> +#include <qqml.h> + +class ForeignDisplay : public QObject +{ + Q_OBJECT + QML_NAMED_ELEMENT(ThirdPartyDisplay) + QML_FOREIGN(ThirdPartyDisplay) +}; + +#endif // FOREIGNDISPLAY_H diff --git a/examples/qml/referenceexamples/valuesource/happybirthdaysong.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/happybirthdaysong.cpp index e9a8c13ae9..7a756a4a71 100644 --- a/examples/qml/referenceexamples/valuesource/happybirthdaysong.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/happybirthdaysong.cpp @@ -1,20 +1,20 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "happybirthdaysong.h" + #include <QTimer> -HappyBirthdaySong::HappyBirthdaySong(QObject *parent) : - QObject(parent) +HappyBirthdaySong::HappyBirthdaySong(QObject *parent) : QObject(parent) { auto *timer = new QTimer(this); QObject::connect(timer, &QTimer::timeout, this, &HappyBirthdaySong::advance); timer->start(1000); } -void HappyBirthdaySong::setTarget(const QQmlProperty &p) +void HappyBirthdaySong::setTarget(const QQmlProperty &target) { - m_target = p; + m_target = target; } QString HappyBirthdaySong::name() const @@ -24,7 +24,10 @@ QString HappyBirthdaySong::name() const void HappyBirthdaySong::setName(const QString &name) { - m_name = name; + if (m_name != name) { + m_name = name; + emit nameChanged(); + } m_lyrics.clear(); m_lyrics << "Happy birthday to you,"; @@ -40,4 +43,3 @@ void HappyBirthdaySong::advance() m_target.write(m_lyrics.at(m_line)); } - diff --git a/examples/qml/referenceexamples/valuesource/happybirthdaysong.h b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/happybirthdaysong.h index 1aad47232a..f87521a760 100644 --- a/examples/qml/referenceexamples/valuesource/happybirthdaysong.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/happybirthdaysong.h @@ -1,4 +1,4 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef HAPPYBIRTHDAYSONG_H @@ -7,27 +7,25 @@ #include <QQmlPropertyValueSource> #include <QQmlProperty> #include <qqml.h> - #include <QStringList> -// ![0] class HappyBirthdaySong : public QObject, public QQmlPropertyValueSource { Q_OBJECT Q_INTERFACES(QQmlPropertyValueSource) -// ![0] - Q_PROPERTY(QString name READ name WRITE setName) -// ![1] + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) QML_ELEMENT public: explicit HappyBirthdaySong(QObject *parent = nullptr); void setTarget(const QQmlProperty &) override; -// ![1] QString name() const; void setName(const QString &); +signals: + void nameChanged(); + private slots: void advance(); @@ -36,9 +34,6 @@ private: QStringList m_lyrics; QQmlProperty m_target; QString m_name; -// ![2] }; -// ![2] #endif // HAPPYBIRTHDAYSONG_H - diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/CMakeLists.txt b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/CMakeLists.txt new file mode 100644 index 0000000000..5b4528a340 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) + +project(library) + +add_library(library ThirdPartyDisplay.cpp ThirdPartyDisplay.h) + +qt_extract_metatypes(library) + +target_link_libraries(library PUBLIC + Qt::Core + Qt::Qml + Qt::Quick + Qt::Gui +) diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/ThirdPartyDisplay.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/ThirdPartyDisplay.cpp new file mode 100644 index 0000000000..5fc4eb2e8f --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/ThirdPartyDisplay.cpp @@ -0,0 +1,45 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "ThirdPartyDisplay.h" +#include <QDebug> + +const QString &ThirdPartyDisplay::content() const +{ + return m_content; +} + +void ThirdPartyDisplay::setContent(const QString &content) +{ + if (m_content != content) { + m_content = content; + emit contentChanged(); + } + qInfo() << QStringLiteral("[Fancy ThirdPartyDisplay] ") + content; +} + +QColor ThirdPartyDisplay::foregroundColor() const +{ + return m_foregroundColor; +} + +void ThirdPartyDisplay::setForegroundColor(QColor color) +{ + if (m_foregroundColor != color) { + m_foregroundColor = color; + emit colorsChanged(); + } +} + +QColor ThirdPartyDisplay::backgroundColor() const +{ + return m_backgroundColor; +} + +void ThirdPartyDisplay::setBackgroundColor(QColor color) +{ + if (m_backgroundColor != color) { + m_backgroundColor = color; + emit colorsChanged(); + } +} diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/ThirdPartyDisplay.h b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/ThirdPartyDisplay.h new file mode 100644 index 0000000000..525c9f72cf --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/ThirdPartyDisplay.h @@ -0,0 +1,36 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef THIRDPARTYDISPLAY_H +#define THIRDPARTYDISPLAY_H + +#include <QColor> +#include <QObject> + +class Q_DECL_EXPORT ThirdPartyDisplay : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString content READ content WRITE setContent NOTIFY contentChanged FINAL) + Q_PROPERTY(QColor foregroundColor READ foregroundColor WRITE setForegroundColor NOTIFY colorsChanged FINAL) + Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY colorsChanged FINAL) +public: + const QString &content() const; + void setContent(const QString &content); + + QColor foregroundColor() const; + void setForegroundColor(QColor); + + QColor backgroundColor() const; + void setBackgroundColor(QColor); + +signals: + void contentChanged(); + void colorsChanged(); + +private: + QString m_content; + QColor m_foregroundColor; + QColor m_backgroundColor; +}; + +#endif // THIRDPARTYDISPLAY_H diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/library.pro b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/library.pro new file mode 100644 index 0000000000..f7009c46c9 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/library/library.pro @@ -0,0 +1,8 @@ +TEMPLATE = lib + +CONFIG += static + +SOURCES += ThirdPartyDisplay.cpp +HEADERS += ThirdPartyDisplay.h + +QT += core qml gui diff --git a/examples/qml/referenceexamples/binding/main.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/main.cpp index 716deeccda..9c6f6bcc2f 100644 --- a/examples/qml/referenceexamples/binding/main.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/main.cpp @@ -1,20 +1,23 @@ -// Copyright (C) 2017 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "birthdayparty.h" +#include "person.h" + #include <QCoreApplication> #include <QQmlEngine> #include <QQmlComponent> #include <QDebug> -#include "birthdayparty.h" -#include "happybirthdaysong.h" -#include "person.h" +#include <QFile> -int main(int argc, char ** argv) +int main(int argc, char **argv) { QCoreApplication app(argc, argv); QQmlEngine engine; - QQmlComponent component(&engine, QUrl("qrc:example.qml")); - auto *party = qobject_cast<BirthdayParty *>(component.create()); + QQmlComponent component(&engine); + component.loadFromModule("People", "Main"); + std::unique_ptr<BirthdayParty> party{ qobject_cast<BirthdayParty *>(component.create()) }; if (party && party->host()) { qInfo() << party->host()->name() << "is having a birthday!"; @@ -28,8 +31,7 @@ int main(int argc, char ** argv) Person *guest = party->guest(ii); QDate rsvpDate; - QObject *attached = - qmlAttachedPropertiesObject<BirthdayParty>(guest, false); + QObject *attached = qmlAttachedPropertiesObject<BirthdayParty>(guest, false); if (attached) rsvpDate = attached->property("rsvp").toDate(); diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.cpp new file mode 100644 index 0000000000..fe3d19b58d --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.cpp @@ -0,0 +1,87 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "person.h" + +Person::Person(QObject *parent) : QObject(parent) +{ + m_shoe = new ShoeDescription(this); +} + +int ShoeDescription::size() const +{ + return m_size; +} + +void ShoeDescription::setSize(int size) +{ + if (m_size != size) { + m_size = size; + emit shoeChanged(); + } +} + +QColor ShoeDescription::color() const +{ + return m_color; +} + +void ShoeDescription::setColor(const QColor &color) +{ + if (m_color != color) { + m_color = color; + emit shoeChanged(); + } +} + +QString ShoeDescription::brand() const +{ + return m_brand; +} + +void ShoeDescription::setBrand(const QString &brand) +{ + if (m_brand != brand) { + m_brand = brand; + emit shoeChanged(); + } +} + +qreal ShoeDescription::price() const +{ + return m_price; +} + +void ShoeDescription::setPrice(qreal price) +{ + if (m_price != price) { + m_price = price; + emit shoeChanged(); + } +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString &name) +{ + if (m_name != name) { + m_name = name; + emit nameChanged(); + } +} + +ShoeDescription *Person::shoe() const +{ + return m_shoe; +} + +void Person::setShoe(ShoeDescription *shoe) +{ + if (m_shoe != shoe) { + m_shoe = shoe; + emit shoeChanged(); + } +} diff --git a/examples/qml/referenceexamples/grouped/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.h index b5fa469025..0ed76223ba 100644 --- a/examples/qml/referenceexamples/grouped/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.h @@ -1,20 +1,20 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef PERSON_H #define PERSON_H +#include <QtQml/qqml.h> #include <QObject> #include <QColor> -#include <QtQml/qqml.h> class ShoeDescription : public QObject { Q_OBJECT - Q_PROPERTY(int size READ size WRITE setSize) - Q_PROPERTY(QColor color READ color WRITE setColor) - Q_PROPERTY(QString brand READ brand WRITE setBrand) - Q_PROPERTY(qreal price READ price WRITE setPrice) + Q_PROPERTY(int size READ size WRITE setSize NOTIFY shoeChanged FINAL) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY shoeChanged FINAL) + Q_PROPERTY(QString brand READ brand WRITE setBrand NOTIFY shoeChanged FINAL) + Q_PROPERTY(qreal price READ price WRITE setPrice NOTIFY shoeChanged FINAL) QML_ANONYMOUS public: using QObject::QObject; @@ -31,6 +31,9 @@ public: qreal price() const; void setPrice(qreal); +signals: + void shoeChanged(); + private: int m_size = 0; QColor m_color; @@ -41,21 +44,28 @@ private: class Person : public QObject { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) -// ![1] - Q_PROPERTY(ShoeDescription *shoe READ shoe) -// ![1] - QML_ANONYMOUS + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) + Q_PROPERTY(ShoeDescription *shoe READ shoe WRITE setShoe NOTIFY shoeChanged FINAL) + QML_ELEMENT + QML_UNCREATABLE("Person is an abstract base class.") public: using QObject::QObject; + Person(QObject *parent = nullptr); + QString name() const; void setName(const QString &); - ShoeDescription *shoe(); + ShoeDescription *shoe() const; + void setShoe(ShoeDescription *shoe); + +signals: + void nameChanged(); + void shoeChanged(); + private: QString m_name; - ShoeDescription m_shoe; + ShoeDescription *m_shoe = nullptr; }; class Boy : public Person diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/qmldir.in b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/qmldir.in new file mode 100644 index 0000000000..5289a31938 --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/qmldir.in @@ -0,0 +1,5 @@ +module People +typeinfo foreign.qmltypes +prefer :/qt/qml/People/ +Main 254.0 Main.qml +depends QtQuick diff --git a/examples/qml/tutorials/extending-qml-advanced/extending-qml-advanced.pro b/examples/qml/tutorials/extending-qml-advanced/extending-qml-advanced.pro new file mode 100644 index 0000000000..387d880a7d --- /dev/null +++ b/examples/qml/tutorials/extending-qml-advanced/extending-qml-advanced.pro @@ -0,0 +1,10 @@ +TEMPLATE = subdirs + +SUBDIRS += \ + advanced1-Base-project \ + advanced2-Inheritance-and-coercion \ + advanced3-Default-properties \ + advanced4-Grouped-properties + advanced5-Attached-properties \ + advanced6-Property-value-source \ + advanced7-Extension-objects \ |
