diff options
author | Øystein Heskestad <oystein.heskestad@qt.io> | 2023-07-10 12:20:50 +0200 |
---|---|---|
committer | Øystein Heskestad <oystein.heskestad@qt.io> | 2023-09-14 18:27:09 +0200 |
commit | cad16240256a2b06dd933af30f77787315424b25 (patch) | |
tree | 87563ed8a96a29d63389d0445847b595a4b8ed2a /examples | |
parent | 3f6a4b27b3056aeb2ea49d9ff73ba90d9f06c328 (diff) |
Revamp clientapp example to prefer QML types over C++ classes
Replace C++ with more succinct QML.
Task-number: QTBUG-112850
Change-Id: Ibd3ddcafdee0e6a47e83612cd11873a1656e7844
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/remoteobjects/clientapp/CMakeLists.txt | 35 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/Clock.qml (renamed from examples/remoteobjects/clientapp/qml/Clock.qml) | 12 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/Plugins.qml (renamed from examples/remoteobjects/clientapp/qml/plugins.qml) | 10 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/center.png (renamed from examples/remoteobjects/clientapp/qml/center.png) | bin | 765 -> 765 bytes | |||
-rw-r--r-- | examples/remoteobjects/clientapp/clientapp.pro | 22 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/clientapp.qrc | 13 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/clock.png (renamed from examples/remoteobjects/clientapp/qml/clock.png) | bin | 20653 -> 20653 bytes | |||
-rw-r--r-- | examples/remoteobjects/clientapp/hour.png (renamed from examples/remoteobjects/clientapp/qml/hour.png) | bin | 625 -> 625 bytes | |||
-rw-r--r-- | examples/remoteobjects/clientapp/main.cpp | 13 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/minute.png (renamed from examples/remoteobjects/clientapp/qml/minute.png) | bin | 625 -> 625 bytes | |||
-rw-r--r-- | examples/remoteobjects/clientapp/plugin.cpp | 35 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/plugin.h | 35 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/plugins0.qml (renamed from examples/remoteobjects/clientapp/qml/plugins0.qml) | 11 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/plugins1.qml (renamed from examples/remoteobjects/clientapp/qml/plugins1.qml) | 13 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/plugins2.qml (renamed from examples/remoteobjects/clientapp/qml/plugins2.qml) | 18 | ||||
-rw-r--r-- | examples/remoteobjects/clientapp/qmldir | 4 |
16 files changed, 86 insertions, 135 deletions
diff --git a/examples/remoteobjects/clientapp/CMakeLists.txt b/examples/remoteobjects/clientapp/CMakeLists.txt index 63a3b3f..f7f1914 100644 --- a/examples/remoteobjects/clientapp/CMakeLists.txt +++ b/examples/remoteobjects/clientapp/CMakeLists.txt @@ -23,18 +23,22 @@ qt_add_executable(clientapp main.cpp ) -qt6_add_qml_module(clientapp +qt_add_qml_module(clientapp VERSION 1.0 URI "TimeExample" SOURCES - plugin.h plugin.cpp + plugin.h QML_FILES - qml/Clock.qml + Clock.qml + Plugins.qml + plugins0.qml + plugins1.qml + plugins2.qml RESOURCES - qml/center.png - qml/clock.png - qml/hour.png - qml/minute.png + center.png + clock.png + hour.png + minute.png ) target_link_libraries(clientapp PRIVATE @@ -44,22 +48,7 @@ target_link_libraries(clientapp PRIVATE Qt::RemoteObjects ) -# Resources: -set(clientapp_resource_files - "qml/plugins.qml" - "qml/plugins0.qml" - "qml/plugins1.qml" - "qml/plugins2.qml" -) - -qt6_add_resources(clientapp "clientapp" - PREFIX - "/qml" - FILES - ${clientapp_resource_files} -) - -qt6_add_repc_replicas(clientapp +qt_add_repc_replicas(clientapp ../timemodel.rep ) diff --git a/examples/remoteobjects/clientapp/qml/Clock.qml b/examples/remoteobjects/clientapp/Clock.qml index b620321..f5f6a08 100644 --- a/examples/remoteobjects/clientapp/qml/Clock.qml +++ b/examples/remoteobjects/clientapp/Clock.qml @@ -7,11 +7,9 @@ Rectangle { id: clock width: 200; height: 200; color: "gray" - property alias city: cityLabel.text - property variant hours - property variant minutes - property variant shift : 0 - property variant valid : true + property int hours + property int minutes + property bool valid : true Image { id: background; source: "clock.png" } @@ -46,10 +44,6 @@ Rectangle { } Text { - id: cityLabel; font.bold: true; font.pixelSize: 14; y:200; color: "white" - anchors.horizontalCenter: parent.horizontalCenter - } - Text { id: validity; font.bold: true; font.pixelSize: 14; y:100; color: "black" anchors.horizontalCenter: parent.horizontalCenter text: clock.valid ? "" : "Server not found" diff --git a/examples/remoteobjects/clientapp/qml/plugins.qml b/examples/remoteobjects/clientapp/Plugins.qml index 2f608ad..5273352 100644 --- a/examples/remoteobjects/clientapp/qml/plugins.qml +++ b/examples/remoteobjects/clientapp/Plugins.qml @@ -3,17 +3,18 @@ import QtQuick -Item { +Window { + id: window width: 200 height: 400 + visible: true property int counter: 0; MouseArea { anchors.fill: parent onClicked: { - counter = (counter + 1) % 3; - console.log(counter); - pageLoader.source = "plugins"+counter+".qml" + window.counter = (window.counter + 1) % 3; + pageLoader.source = "plugins"+window.counter+".qml" } } Loader { @@ -21,4 +22,3 @@ Item { source: "plugins0.qml" } } -//![0] diff --git a/examples/remoteobjects/clientapp/qml/center.png b/examples/remoteobjects/clientapp/center.png Binary files differindex 7fbd802..7fbd802 100644 --- a/examples/remoteobjects/clientapp/qml/center.png +++ b/examples/remoteobjects/clientapp/center.png diff --git a/examples/remoteobjects/clientapp/clientapp.pro b/examples/remoteobjects/clientapp/clientapp.pro index 4808fe5..d6db375 100644 --- a/examples/remoteobjects/clientapp/clientapp.pro +++ b/examples/remoteobjects/clientapp/clientapp.pro @@ -1,17 +1,33 @@ +TEMPLATE = app +TARGET = clientapp + QT += remoteobjects quick CONFIG += qmltypes REPC_REPLICA += $$PWD/../timemodel.rep -TARGET = clientapp QML_IMPORT_NAME = TimeExample QML_IMPORT_MAJOR_VERSION = 1 -SOURCES += main.cpp plugin.cpp +SOURCES += main.cpp HEADERS += plugin.h -RESOURCES += clientapp.qrc +qml_resources.files = \ + qmldir \ + Plugins.qml \ + plugins0.qml \ + plugins1.qml \ + plugins2.qml \ + Clock.qml \ + center.png \ + clock.png \ + hour.png \ + minute.png + +qml_resources.prefix = /qt/qml/TimeExample + +RESOURCES += qml_resources contains(QT_CONFIG, c++11): CONFIG += c++11 diff --git a/examples/remoteobjects/clientapp/clientapp.qrc b/examples/remoteobjects/clientapp/clientapp.qrc deleted file mode 100644 index 5003239..0000000 --- a/examples/remoteobjects/clientapp/clientapp.qrc +++ /dev/null @@ -1,13 +0,0 @@ -<RCC> - <qresource prefix="/qml"> - <file>qml/plugins0.qml</file> - <file>qml/plugins.qml</file> - <file>qml/plugins1.qml</file> - <file>qml/plugins2.qml</file> - <file>qml/Clock.qml</file> - <file>qml/center.png</file> - <file>qml/clock.png</file> - <file>qml/hour.png</file> - <file>qml/minute.png</file> - </qresource> -</RCC> diff --git a/examples/remoteobjects/clientapp/qml/clock.png b/examples/remoteobjects/clientapp/clock.png Binary files differindex 462edac..462edac 100644 --- a/examples/remoteobjects/clientapp/qml/clock.png +++ b/examples/remoteobjects/clientapp/clock.png diff --git a/examples/remoteobjects/clientapp/qml/hour.png b/examples/remoteobjects/clientapp/hour.png Binary files differindex f8061a1..f8061a1 100644 --- a/examples/remoteobjects/clientapp/qml/hour.png +++ b/examples/remoteobjects/clientapp/hour.png diff --git a/examples/remoteobjects/clientapp/main.cpp b/examples/remoteobjects/clientapp/main.cpp index d4e5ba8..df2aca1 100644 --- a/examples/remoteobjects/clientapp/main.cpp +++ b/examples/remoteobjects/clientapp/main.cpp @@ -2,17 +2,16 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include <QtGui/QGuiApplication> -#include <QtQml/QQmlEngine> -#include <QtQuick/QQuickView> +#include <QtQml/QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); - QQuickView viewer; - viewer.engine()->addImportPath(QStringLiteral("qrc:/qml")); - viewer.setSource(QUrl(QStringLiteral("qrc:/qml/qml/plugins.qml"))); - viewer.show(); - + QQmlApplicationEngine engine; + QObject::connect( + &engine, &QQmlApplicationEngine::objectCreationFailed, &app, + []() { QCoreApplication::exit(1); }, Qt::QueuedConnection); + engine.loadFromModule("TimeExample", "Plugins"); return app.exec(); } diff --git a/examples/remoteobjects/clientapp/qml/minute.png b/examples/remoteobjects/clientapp/minute.png Binary files differindex 1297ec7..1297ec7 100644 --- a/examples/remoteobjects/clientapp/qml/minute.png +++ b/examples/remoteobjects/clientapp/minute.png diff --git a/examples/remoteobjects/clientapp/plugin.cpp b/examples/remoteobjects/clientapp/plugin.cpp deleted file mode 100644 index f028f95..0000000 --- a/examples/remoteobjects/clientapp/plugin.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (C) 2017 Ford Motor Company -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "plugin.h" - -// Implements a "TimeModel" class with hour and minute properties -// that change on-the-minute yet efficiently sleep the rest -// of the time. - -TimeModel::TimeModel(QObject *parent) : QObject(parent), m_replica(nullptr) -{ - m_client.setRegistryUrl(QUrl(QStringLiteral("local:registry"))); - m_replica.reset(m_client.acquire<MinuteTimerReplica>()); - connect(m_replica.get(), &MinuteTimerReplica::hourChanged, this, &TimeModel::timeChanged); - connect(m_replica.get(), &MinuteTimerReplica::minuteChanged, this, &TimeModel::timeChanged); - connect(m_replica.get(), &MinuteTimerReplica::timeChanged, this, &TimeModel::timeChanged); - connect(m_replica.get(), &MinuteTimerReplica::stateChanged, this, &TimeModel::isValidChanged); -} - -TimeModel::~TimeModel() { } - -int TimeModel::minute() const -{ - return m_replica->minute(); -} - -int TimeModel::hour() const -{ - return m_replica->hour(); -} - -bool TimeModel::isValid() const -{ - return m_replica->state() == QRemoteObjectReplica::Valid; -} diff --git a/examples/remoteobjects/clientapp/plugin.h b/examples/remoteobjects/clientapp/plugin.h index e48e69f..848c474 100644 --- a/examples/remoteobjects/clientapp/plugin.h +++ b/examples/remoteobjects/clientapp/plugin.h @@ -1,42 +1,17 @@ // Copyright (C) 2017 Ford Motor Company // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QtQml/QQmlExtensionPlugin> -#include <QtQml/QQmlEngine> -#include <QtQml/qqml.h> -#include <QDebug> -#include <QDateTime> -#include <QBasicTimer> -#include <QCoreApplication> -#include <QRemoteObjectReplica> -#include <QRemoteObjectNode> +#include <QObject> +#include <QQmlEngine> #include "rep_timemodel_replica.h" // A "TimeModel" class with hour and minute properties // that change on-the-minute yet efficiently sleep the rest // of the time. -class TimeModel : public QObject +struct TimeModel { - Q_OBJECT - Q_PROPERTY(int hour READ hour NOTIFY timeChanged) - Q_PROPERTY(int minute READ minute NOTIFY timeChanged) - Q_PROPERTY(bool isValid READ isValid NOTIFY isValidChanged) + Q_GADGET + QML_FOREIGN(MinuteTimerReplica) QML_NAMED_ELEMENT(Time) - -public: - TimeModel(QObject *parent = nullptr); - ~TimeModel() override; - - int minute() const; - int hour() const; - bool isValid() const; - -signals: - void timeChanged(); - void isValidChanged(); - -private: - QRemoteObjectNode m_client; - std::unique_ptr<MinuteTimerReplica> m_replica; }; diff --git a/examples/remoteobjects/clientapp/qml/plugins0.qml b/examples/remoteobjects/clientapp/plugins0.qml index 32bc3cf..99b132a 100644 --- a/examples/remoteobjects/clientapp/qml/plugins0.qml +++ b/examples/remoteobjects/clientapp/plugins0.qml @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause //![0] import QtQuick +import QtRemoteObjects import TimeExample // import types from the plugin Rectangle { @@ -9,14 +10,20 @@ Rectangle { height: 400 color: "blue" - Time { // this class is defined in C++ (plugin.cpp) + Node { + id: node + registryUrl: "local:registry" + } + + Time { // this class is defined in C++ id: time + node: node } Text { id: validity; font.bold: true; font.pixelSize: 14; y:200; color: "white" anchors.horizontalCenter: parent.horizontalCenter - text: time.isValid ? "Click to see clock!" : "Server not found" + text: time.state == Time.Valid ? "Click to see a clock!" : "Server not found" } } //![0] diff --git a/examples/remoteobjects/clientapp/qml/plugins1.qml b/examples/remoteobjects/clientapp/plugins1.qml index 56c33a9..a7de87e 100644 --- a/examples/remoteobjects/clientapp/qml/plugins1.qml +++ b/examples/remoteobjects/clientapp/plugins1.qml @@ -1,6 +1,7 @@ // Copyright (C) 2017 Ford Motor Company // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause //![0] +import QtRemoteObjects import QtQuick import TimeExample // import types from the plugin @@ -11,14 +12,20 @@ Rectangle { Clock { id: clock1 anchors.top: parent.top - Time { // this class is defined in C++ (plugin.cpp) + + Time { // this class is defined in C++ id: time + node: node + } + + Node { + id: node + registryUrl: "local:registry" } hours: time.hour minutes: time.minute - valid: time.isValid + valid: time.state == Time.Valid } } -//![0] diff --git a/examples/remoteobjects/clientapp/qml/plugins2.qml b/examples/remoteobjects/clientapp/plugins2.qml index 2e82382..0656432 100644 --- a/examples/remoteobjects/clientapp/qml/plugins2.qml +++ b/examples/remoteobjects/clientapp/plugins2.qml @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause //![0] import QtQuick +import QtRemoteObjects import TimeExample // import types from the plugin Rectangle { @@ -10,26 +11,35 @@ Rectangle { Clock { // this class is defined in QML id: clock1 anchors.top: parent.top - Time { // this class is defined in C++ (plugin.cpp) + + Time { // this class is defined in C++ id: time + node: sharedNode } hours: time.hour minutes: time.minute - valid: time.isValid + valid: time.state == Time.Valid } Clock { // this class is defined in QML id: clock2 anchors.top: clock1.bottom - Time { // this class is defined in C++ (plugin.cpp) + + Time { // this class is defined in C++ id: time2 + node: sharedNode } hours: time2.hour minutes: time2.minute - valid: time2.isValid + valid: time2.state == Time.Valid + + } + Node { + id: sharedNode + registryUrl: "local:registry" } } diff --git a/examples/remoteobjects/clientapp/qmldir b/examples/remoteobjects/clientapp/qmldir index e1a33a2..6c6e152 100644 --- a/examples/remoteobjects/clientapp/qmldir +++ b/examples/remoteobjects/clientapp/qmldir @@ -1,3 +1,5 @@ module TimeExample +typeinfo clientapp.qmltypes +prefer :/qt/qml/TimeExample/ Clock 1.0 Clock.qml -plugin clientapp +Plugins 1.0 Plugins.qml |