diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-06-14 13:39:25 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2023-06-21 10:31:05 +0200 |
commit | cb3d9c032cd5319120df78ce5b9d11b4a3cd9855 (patch) | |
tree | 010d3b25a52db88760b3c078588dcce3dddf189f /examples | |
parent | b6e7a5770add4c183222541ed5043b8f218029be (diff) |
CoAP Multicast Discovery example: use modern C++ type registration in QML
... and also update build files to use QML modules
Task-number: QTBUG-113858
Pick-to: 6.6 6.5
Change-Id: I6dbbe05abc0ec345f923f5b2ff5c95cdacda234c
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/coap/quickmulticastclient/CMakeLists.txt | 20 | ||||
-rw-r--r-- | examples/coap/quickmulticastclient/Main.qml (renamed from examples/coap/quickmulticastclient/main.qml) | 3 | ||||
-rw-r--r-- | examples/coap/quickmulticastclient/main.cpp | 21 | ||||
-rw-r--r-- | examples/coap/quickmulticastclient/qml.qrc | 5 | ||||
-rw-r--r-- | examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp | 1 | ||||
-rw-r--r-- | examples/coap/quickmulticastclient/qmlcoapmulticastclient.h | 13 | ||||
-rw-r--r-- | examples/coap/quickmulticastclient/qmldir | 3 | ||||
-rw-r--r-- | examples/coap/quickmulticastclient/quickmulticastclient.pro | 12 |
8 files changed, 40 insertions, 38 deletions
diff --git a/examples/coap/quickmulticastclient/CMakeLists.txt b/examples/coap/quickmulticastclient/CMakeLists.txt index 76e90d0..dacf3e2 100644 --- a/examples/coap/quickmulticastclient/CMakeLists.txt +++ b/examples/coap/quickmulticastclient/CMakeLists.txt @@ -12,11 +12,10 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/coap/quickmulticastclient") find_package(Qt6 REQUIRED COMPONENTS Coap Core Gui Qml Quick) -qt_standard_project_setup() +qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(quickmulticastclient main.cpp - qmlcoapmulticastclient.cpp qmlcoapmulticastclient.h ) set_target_properties(quickmulticastclient PROPERTIES @@ -32,16 +31,13 @@ target_link_libraries(quickmulticastclient PRIVATE Qt6::Quick ) -# Resources: -set(qml_resource_files - "main.qml" -) - -qt6_add_resources(quickmulticastclient "qml" - PREFIX - "/" - FILES - ${qml_resource_files} +qt_add_qml_module(quickmulticastclient + URI CoapClientModule + VERSION 1.0 + SOURCES + qmlcoapmulticastclient.cpp qmlcoapmulticastclient.h + QML_FILES + Main.qml ) install(TARGETS quickmulticastclient diff --git a/examples/coap/quickmulticastclient/main.qml b/examples/coap/quickmulticastclient/Main.qml index c1c8a6c..3099d4e 100644 --- a/examples/coap/quickmulticastclient/main.qml +++ b/examples/coap/quickmulticastclient/Main.qml @@ -5,8 +5,7 @@ import QtQuick import QtQuick.Window import QtQuick.Controls import QtQuick.Layouts -import CoapMulticastClient -import qtcoap.example.namespace +import CoapClientModule Window { visible: true diff --git a/examples/coap/quickmulticastclient/main.cpp b/examples/coap/quickmulticastclient/main.cpp index e686db8..ee1189a 100644 --- a/examples/coap/quickmulticastclient/main.cpp +++ b/examples/coap/quickmulticastclient/main.cpp @@ -1,8 +1,6 @@ // Copyright (C) 2019 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include "qmlcoapmulticastclient.h" - #include <QGuiApplication> #include <QQmlApplicationEngine> @@ -13,21 +11,10 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); QQmlApplicationEngine engine; - - qmlRegisterType<QmlCoapMulticastClient>("CoapMulticastClient", 1, 0, "CoapMulticastClient"); - - // Register the QtCoap namespace - qmlRegisterUncreatableMetaObject(QtCoap::staticMetaObject, "qtcoap.example.namespace", 1, 0, - "QtCoap", "Access to enums is read-only"); - - const QUrl url(QStringLiteral("qrc:/main.qml")); - QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, - &app, [url](QObject *obj, const QUrl &objUrl) { - // Exit with error, if the object for main.qml could not be loaded. - if (!obj && url == objUrl) - QCoreApplication::exit(-1); - }, Qt::QueuedConnection); - engine.load(url); + QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, + &app, []() { QCoreApplication::exit(1); }, + Qt::QueuedConnection); + engine.loadFromModule("CoapClientModule", "Main"); return app.exec(); } diff --git a/examples/coap/quickmulticastclient/qml.qrc b/examples/coap/quickmulticastclient/qml.qrc deleted file mode 100644 index 5f6483a..0000000 --- a/examples/coap/quickmulticastclient/qml.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<RCC> - <qresource prefix="/"> - <file>main.qml</file> - </qresource> -</RCC> diff --git a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp index 15d3f88..8ab93d8 100644 --- a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp +++ b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp @@ -3,7 +3,6 @@ #include "qmlcoapmulticastclient.h" -#include <QCoapResourceDiscoveryReply> #include <QLoggingCategory> Q_LOGGING_CATEGORY(lcCoapClient, "qt.coap.client") diff --git a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h index 1ffc095..5d4943e 100644 --- a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h +++ b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h @@ -4,8 +4,12 @@ #ifndef QMLCOAPMULTICASTCLIENT_H #define QMLCOAPMULTICASTCLIENT_H +#include <QtCoap/qcoapnamespace.h> #include <QCoapClient> #include <QCoapResource> +#include <QCoapResourceDiscoveryReply> + +#include <QtQml/qqmlregistration.h> class QmlCoapResource : public QCoapResource { @@ -14,6 +18,7 @@ class QmlCoapResource : public QCoapResource Q_PROPERTY(QString host READ hostStr) Q_PROPERTY(QString path READ path) + QML_ANONYMOUS public: QmlCoapResource() : QCoapResource() {} QmlCoapResource(const QCoapResource &resource) @@ -27,6 +32,7 @@ class QmlCoapMulticastClient : public QCoapClient { Q_OBJECT + QML_NAMED_ELEMENT(CoapMulticastClient) public: QmlCoapMulticastClient(QObject *parent = nullptr); @@ -41,4 +47,11 @@ public slots: void onDiscovered(QCoapResourceDiscoveryReply *reply, const QList<QCoapResource> &resources); }; +namespace QCoapForeignNamespace +{ + Q_NAMESPACE + QML_FOREIGN_NAMESPACE(QtCoap) + QML_NAMED_ELEMENT(QtCoap) +} + #endif // QMLCOAPMULTICASTCLIENT_H diff --git a/examples/coap/quickmulticastclient/qmldir b/examples/coap/quickmulticastclient/qmldir new file mode 100644 index 0000000..09b2eff --- /dev/null +++ b/examples/coap/quickmulticastclient/qmldir @@ -0,0 +1,3 @@ +module CoapClientModule +prefer :/qt/qml/CoapClientModule/ +Main 1.0 Main.qml diff --git a/examples/coap/quickmulticastclient/quickmulticastclient.pro b/examples/coap/quickmulticastclient/quickmulticastclient.pro index 6ac4c76..8ab90a1 100644 --- a/examples/coap/quickmulticastclient/quickmulticastclient.pro +++ b/examples/coap/quickmulticastclient/quickmulticastclient.pro @@ -2,6 +2,10 @@ TEMPLATE = app QT += qml quick coap +CONFIG += qmltypes +QML_IMPORT_NAME = CoapClientModule +QML_IMPORT_MAJOR_VERSION = 1 + SOURCES += \ main.cpp \ qmlcoapmulticastclient.cpp @@ -9,7 +13,13 @@ SOURCES += \ HEADERS += \ qmlcoapmulticastclient.h -RESOURCES += qml.qrc +qml_resources.files = \ + qmldir \ + Main.qml + +qml_resources.prefix = /qt/qml/CoapClientModule + +RESOURCES += qml_resources target.path = $$[QT_INSTALL_EXAMPLES]/coap/quickmulticastclient INSTALLS += target |