From c883c95561b2206c25eeefe89cac96da1b4de307 Mon Sep 17 00:00:00 2001 From: Mate Barany Date: Mon, 5 Jun 2023 18:45:09 +0200 Subject: QuickSubscription example: Port CMakeLists.txt to use qt_add_qml_module Port the CmakeLists.txt and change the source files accordingly: - rename main.qml to Main.qml - remove MqttClient import from Main.qml - add QML macros to qmlmqttclient.h - revamp main.cpp Also update the qmake build: - revamp quicksubscription.pro - add qmldir to project - remove qml.qrc Task-number: QTBUG-110893 Change-Id: Ie7c0dd260e94c1e1ad4756fb21a70f24f2c60f87 Reviewed-by: Qt CI Bot Reviewed-by: Oliver Eftevaag Reviewed-by: Fabian Kosmale (cherry picked from commit 03cc0dd51271862653fa1c8c85a093a77b5659e3) --- examples/mqtt/quicksubscription/CMakeLists.txt | 30 ++-- examples/mqtt/quicksubscription/Main.qml | 155 ++++++++++++++++++++ examples/mqtt/quicksubscription/main.cpp | 19 ++- examples/mqtt/quicksubscription/main.qml | 156 --------------------- examples/mqtt/quicksubscription/qml.qrc | 5 - examples/mqtt/quicksubscription/qmldir | 4 + examples/mqtt/quicksubscription/qmlmqttclient.h | 4 + .../mqtt/quicksubscription/quicksubscription.pro | 12 +- 8 files changed, 191 insertions(+), 194 deletions(-) create mode 100644 examples/mqtt/quicksubscription/Main.qml delete mode 100644 examples/mqtt/quicksubscription/main.qml delete mode 100644 examples/mqtt/quicksubscription/qml.qrc create mode 100644 examples/mqtt/quicksubscription/qmldir diff --git a/examples/mqtt/quicksubscription/CMakeLists.txt b/examples/mqtt/quicksubscription/CMakeLists.txt index 3c7c892..cbc8e77 100644 --- a/examples/mqtt/quicksubscription/CMakeLists.txt +++ b/examples/mqtt/quicksubscription/CMakeLists.txt @@ -4,8 +4,6 @@ cmake_minimum_required(VERSION 3.16) project(quicksubscription LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() @@ -14,21 +12,19 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/mqtt/quicksubscription") find_package(Qt6 REQUIRED COMPONENTS Core Gui Mqtt Qml Quick) +qt_standard_project_setup(REQUIRES 6.5) + qt_add_executable(quicksubscription + WIN32 + MACOSX_BUNDLE main.cpp - qmlmqttclient.cpp qmlmqttclient.h -) - -set_target_properties(quicksubscription PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE ) target_compile_definitions(quicksubscription PUBLIC QT_DEPRECATED_WARNINGS ) -target_link_libraries(quicksubscription PUBLIC +target_link_libraries(quicksubscription PRIVATE Qt::Core Qt::Gui Qt::Mqtt @@ -36,16 +32,12 @@ target_link_libraries(quicksubscription PUBLIC Qt::Quick ) -# Resources: -set(qml_resource_files - "main.qml" -) - -qt6_add_resources(quicksubscription "qml" - PREFIX - "/" - FILES - ${qml_resource_files} +qt_add_qml_module(quicksubscription + URI subscription + QML_FILES + "Main.qml" + SOURCES + qmlmqttclient.cpp qmlmqttclient.h ) install(TARGETS quicksubscription diff --git a/examples/mqtt/quicksubscription/Main.qml b/examples/mqtt/quicksubscription/Main.qml new file mode 100644 index 0000000..05f8920 --- /dev/null +++ b/examples/mqtt/quicksubscription/Main.qml @@ -0,0 +1,155 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick 2.8 +import QtQuick.Window 2.2 +import QtQuick.Controls 2.1 +import QtQuick.Layouts 1.1 + +Window { + visible: true + width: 640 + height: 480 + title: qsTr("Qt Quick MQTT Subscription Example") + id: root + + property var tempSubscription: 0 + + MqttClient { + id: client + hostname: hostnameField.text + port: portField.text + } + + ListModel { + id: messageModel + } + + function addMessage(payload) + { + messageModel.insert(0, {"payload" : payload}) + + if (messageModel.count >= 100) + messageModel.remove(99) + } + + GridLayout { + anchors.fill: parent + anchors.margins: 10 + columns: 2 + + Label { + text: "Hostname:" + enabled: client.state === MqttClient.Disconnected + } + + TextField { + id: hostnameField + Layout.fillWidth: true + text: "test.mosquitto.org" + placeholderText: "" + enabled: client.state === MqttClient.Disconnected + } + + Label { + text: "Port:" + enabled: client.state === MqttClient.Disconnected + } + + TextField { + id: portField + Layout.fillWidth: true + text: "1883" + placeholderText: "" + inputMethodHints: Qt.ImhDigitsOnly + enabled: client.state === MqttClient.Disconnected + } + + Button { + id: connectButton + Layout.columnSpan: 2 + Layout.fillWidth: true + text: client.state === MqttClient.Connected ? "Disconnect" : "Connect" + onClicked: { + if (client.state === MqttClient.Connected) { + client.disconnectFromHost() + messageModel.clear() + tempSubscription.destroy() + tempSubscription = 0 + } else + client.connectToHost() + } + } + + RowLayout { + enabled: client.state === MqttClient.Connected + Layout.columnSpan: 2 + Layout.fillWidth: true + + Label { + text: "Topic:" + } + + TextField { + id: subField + placeholderText: "" + Layout.fillWidth: true + enabled: tempSubscription === 0 + } + + Button { + id: subButton + text: "Subscribe" + visible: tempSubscription === 0 + onClicked: { + if (subField.text.length === 0) { + console.log("No topic specified to subscribe to.") + return + } + tempSubscription = client.subscribe(subField.text) + tempSubscription.messageReceived.connect(addMessage) + } + } + } + + ListView { + id: messageView + model: messageModel + height: 300 + width: 200 + Layout.columnSpan: 2 + Layout.fillHeight: true + Layout.fillWidth: true + clip: true + delegate: Rectangle { + width: messageView.width + height: 30 + color: index % 2 ? "#DDDDDD" : "#888888" + radius: 5 + Text { + text: payload + anchors.centerIn: parent + } + } + } + + Label { + function stateToString(value) { + if (value === 0) + return "Disconnected" + else if (value === 1) + return "Connecting" + else if (value === 2) + return "Connected" + else + return "Unknown" + } + + Layout.columnSpan: 2 + Layout.fillWidth: true + color: "#333333" + text: "Status:" + stateToString(client.state) + "(" + client.state + ")" + enabled: client.state === MqttClient.Connected + } + } +} diff --git a/examples/mqtt/quicksubscription/main.cpp b/examples/mqtt/quicksubscription/main.cpp index 785140b..b2a59d2 100644 --- a/examples/mqtt/quicksubscription/main.cpp +++ b/examples/mqtt/quicksubscription/main.cpp @@ -1,24 +1,21 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include "qmlmqttclient.h" +#include +#include -#include -#include -#include +using namespace Qt::StringLiterals; int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); - QQmlApplicationEngine engine; - qmlRegisterType("MqttClient", 1, 0, "MqttClient"); - qmlRegisterUncreatableType("MqttClient", 1, 0, "MqttSubscription", QLatin1String("Subscriptions are read-only")); + QObject::connect( + &engine, &QQmlApplicationEngine::objectCreationFailed, &app, + []() { QCoreApplication::exit(EXIT_FAILURE); }, Qt::QueuedConnection); - engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); - if (engine.rootObjects().isEmpty()) - return -1; + engine.loadFromModule(u"subscription"_s, u"Main"_s); - return app.exec(); + return QGuiApplication::exec(); } diff --git a/examples/mqtt/quicksubscription/main.qml b/examples/mqtt/quicksubscription/main.qml deleted file mode 100644 index 7f3c48b..0000000 --- a/examples/mqtt/quicksubscription/main.qml +++ /dev/null @@ -1,156 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -import QtQuick 2.8 -import QtQuick.Window 2.2 -import QtQuick.Controls 2.1 -import QtQuick.Layouts 1.1 -import MqttClient 1.0 - -Window { - visible: true - width: 640 - height: 480 - title: qsTr("Qt Quick MQTT Subscription Example") - id: root - - property var tempSubscription: 0 - - MqttClient { - id: client - hostname: hostnameField.text - port: portField.text - } - - ListModel { - id: messageModel - } - - function addMessage(payload) - { - messageModel.insert(0, {"payload" : payload}) - - if (messageModel.count >= 100) - messageModel.remove(99) - } - - GridLayout { - anchors.fill: parent - anchors.margins: 10 - columns: 2 - - Label { - text: "Hostname:" - enabled: client.state === MqttClient.Disconnected - } - - TextField { - id: hostnameField - Layout.fillWidth: true - text: "test.mosquitto.org" - placeholderText: "" - enabled: client.state === MqttClient.Disconnected - } - - Label { - text: "Port:" - enabled: client.state === MqttClient.Disconnected - } - - TextField { - id: portField - Layout.fillWidth: true - text: "1883" - placeholderText: "" - inputMethodHints: Qt.ImhDigitsOnly - enabled: client.state === MqttClient.Disconnected - } - - Button { - id: connectButton - Layout.columnSpan: 2 - Layout.fillWidth: true - text: client.state === MqttClient.Connected ? "Disconnect" : "Connect" - onClicked: { - if (client.state === MqttClient.Connected) { - client.disconnectFromHost() - messageModel.clear() - tempSubscription.destroy() - tempSubscription = 0 - } else - client.connectToHost() - } - } - - RowLayout { - enabled: client.state === MqttClient.Connected - Layout.columnSpan: 2 - Layout.fillWidth: true - - Label { - text: "Topic:" - } - - TextField { - id: subField - placeholderText: "" - Layout.fillWidth: true - enabled: tempSubscription === 0 - } - - Button { - id: subButton - text: "Subscribe" - visible: tempSubscription === 0 - onClicked: { - if (subField.text.length === 0) { - console.log("No topic specified to subscribe to.") - return - } - tempSubscription = client.subscribe(subField.text) - tempSubscription.messageReceived.connect(addMessage) - } - } - } - - ListView { - id: messageView - model: messageModel - height: 300 - width: 200 - Layout.columnSpan: 2 - Layout.fillHeight: true - Layout.fillWidth: true - clip: true - delegate: Rectangle { - width: messageView.width - height: 30 - color: index % 2 ? "#DDDDDD" : "#888888" - radius: 5 - Text { - text: payload - anchors.centerIn: parent - } - } - } - - Label { - function stateToString(value) { - if (value === 0) - return "Disconnected" - else if (value === 1) - return "Connecting" - else if (value === 2) - return "Connected" - else - return "Unknown" - } - - Layout.columnSpan: 2 - Layout.fillWidth: true - color: "#333333" - text: "Status:" + stateToString(client.state) + "(" + client.state + ")" - enabled: client.state === MqttClient.Connected - } - } -} diff --git a/examples/mqtt/quicksubscription/qml.qrc b/examples/mqtt/quicksubscription/qml.qrc deleted file mode 100644 index 5f6483a..0000000 --- a/examples/mqtt/quicksubscription/qml.qrc +++ /dev/null @@ -1,5 +0,0 @@ - - - main.qml - - diff --git a/examples/mqtt/quicksubscription/qmldir b/examples/mqtt/quicksubscription/qmldir new file mode 100644 index 0000000..fa16d60 --- /dev/null +++ b/examples/mqtt/quicksubscription/qmldir @@ -0,0 +1,4 @@ +module subscription + +prefer :/qt/qml/subscription/ +Main 1.0 Main.qml diff --git a/examples/mqtt/quicksubscription/qmlmqttclient.h b/examples/mqtt/quicksubscription/qmlmqttclient.h index e229288..05a86c4 100644 --- a/examples/mqtt/quicksubscription/qmlmqttclient.h +++ b/examples/mqtt/quicksubscription/qmlmqttclient.h @@ -8,12 +8,15 @@ #include #include +#include + class QmlMqttClient; class QmlMqttSubscription : public QObject { Q_OBJECT Q_PROPERTY(QMqttTopicFilter topic MEMBER m_topic NOTIFY topicChanged) + QML_UNCREATABLE("Not intended to be creatable") public: QmlMqttSubscription(QMqttSubscription *s, QmlMqttClient *c); ~QmlMqttSubscription(); @@ -38,6 +41,7 @@ class QmlMqttClient : public QObject Q_PROPERTY(QString hostname READ hostname WRITE setHostname NOTIFY hostnameChanged) Q_PROPERTY(int port READ port WRITE setPort NOTIFY portChanged) Q_PROPERTY(QMqttClient::ClientState state READ state WRITE setState NOTIFY stateChanged) + QML_NAMED_ELEMENT(MqttClient) public: QmlMqttClient(QObject *parent = nullptr); diff --git a/examples/mqtt/quicksubscription/quicksubscription.pro b/examples/mqtt/quicksubscription/quicksubscription.pro index 474fb39..0b32bfd 100644 --- a/examples/mqtt/quicksubscription/quicksubscription.pro +++ b/examples/mqtt/quicksubscription/quicksubscription.pro @@ -1,7 +1,8 @@ TEMPLATE = app +TARGET = quicksubscription QT += qml quick mqtt -CONFIG += c++11 +CONFIG += qmltypes SOURCES += main.cpp \ qmlmqttclient.cpp @@ -9,10 +10,15 @@ SOURCES += main.cpp \ HEADERS += \ qmlmqttclient.h -RESOURCES += qml.qrc +qml_resources.files = Main.qml qmldir +qml_resources.prefix = /qt/qml/subscription + +RESOURCES += qml_resources # Additional import path used to resolve QML modules in Qt Creator's code model -QML_IMPORT_PATH = +QML_IMPORT_PATH = $$pwd/. +QML_IMPORT_NAME = subscription +QML_IMPORT_MAJOR_VERSION = 1 # Additional import path used to resolve QML modules just for Qt Quick Designer QML_DESIGNER_IMPORT_PATH = -- cgit v1.2.3