From 94cf544ab0590b9f38fda8875f678ca7dcdb1a7b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 12 Aug 2022 11:40:32 +0200 Subject: Bluetooth heartrate-game example: Use modern QML registration Use the modern macros and replace setContextProperty(). Pick-to: 6.4 6.3 Change-Id: If83f8a2dfab13e7c1b3dd18048f633c47709a7b3 Reviewed-by: Juha Vuolle Reviewed-by: Ivan Solovev --- examples/bluetooth/heartrate-game/CMakeLists.txt | 6 ++++++ examples/bluetooth/heartrate-game/connectionhandler.h | 3 +++ examples/bluetooth/heartrate-game/devicefinder.h | 4 ++++ examples/bluetooth/heartrate-game/devicehandler.h | 5 +++++ examples/bluetooth/heartrate-game/heartrate-game.pro | 4 ++++ examples/bluetooth/heartrate-game/main.cpp | 10 +++++----- examples/bluetooth/heartrate-game/qml/main.qml | 5 +++++ 7 files changed, 32 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/bluetooth/heartrate-game/CMakeLists.txt b/examples/bluetooth/heartrate-game/CMakeLists.txt index 924e9fc1..42e36390 100644 --- a/examples/bluetooth/heartrate-game/CMakeLists.txt +++ b/examples/bluetooth/heartrate-game/CMakeLists.txt @@ -37,6 +37,12 @@ target_link_libraries(heartrate-game PUBLIC Qt::Quick ) +qt_add_qml_module(heartrate-game + URI Shared + VERSION 1.0 + NO_RESOURCE_TARGET_PATH +) + if (APPLE) if (IOS) set_target_properties(heartrate-game PROPERTIES diff --git a/examples/bluetooth/heartrate-game/connectionhandler.h b/examples/bluetooth/heartrate-game/connectionhandler.h index 17d33ba9..052def9f 100644 --- a/examples/bluetooth/heartrate-game/connectionhandler.h +++ b/examples/bluetooth/heartrate-game/connectionhandler.h @@ -6,6 +6,8 @@ #include +#include + #include class ConnectionHandler : public QObject @@ -17,6 +19,7 @@ class ConnectionHandler : public QObject Q_PROPERTY(QString address READ address NOTIFY deviceChanged) Q_PROPERTY(bool requiresAddressType READ requiresAddressType CONSTANT) + QML_ELEMENT public: explicit ConnectionHandler(QObject *parent = nullptr); diff --git a/examples/bluetooth/heartrate-game/devicefinder.h b/examples/bluetooth/heartrate-game/devicefinder.h index 6f4226a8..860e6066 100644 --- a/examples/bluetooth/heartrate-game/devicefinder.h +++ b/examples/bluetooth/heartrate-game/devicefinder.h @@ -9,6 +9,8 @@ #include #include +#include + #include #include @@ -22,6 +24,8 @@ class DeviceFinder: public BluetoothBaseClass Q_PROPERTY(bool scanning READ scanning NOTIFY scanningChanged) Q_PROPERTY(QVariant devices READ devices NOTIFY devicesChanged) + QML_ELEMENT + public: DeviceFinder(DeviceHandler *handler, QObject *parent = nullptr); ~DeviceFinder(); diff --git a/examples/bluetooth/heartrate-game/devicehandler.h b/examples/bluetooth/heartrate-game/devicehandler.h index 522f8ee8..7c8aa346 100644 --- a/examples/bluetooth/heartrate-game/devicehandler.h +++ b/examples/bluetooth/heartrate-game/devicehandler.h @@ -9,6 +9,8 @@ #include #include +#include + #include #include #include @@ -29,6 +31,9 @@ class DeviceHandler : public BluetoothBaseClass Q_PROPERTY(float calories READ calories NOTIFY statsChanged) Q_PROPERTY(AddressType addressType READ addressType WRITE setAddressType) + QML_NAMED_ELEMENT(AddressType) + QML_UNCREATABLE("Enum is not a type") + public: enum class AddressType { PublicAddress, diff --git a/examples/bluetooth/heartrate-game/heartrate-game.pro b/examples/bluetooth/heartrate-game/heartrate-game.pro index 02d238ad..f55053b4 100644 --- a/examples/bluetooth/heartrate-game/heartrate-game.pro +++ b/examples/bluetooth/heartrate-game/heartrate-game.pro @@ -4,6 +4,10 @@ TARGET = heartrate-game QT += qml quick bluetooth CONFIG += c++11 +CONFIG += qmltypes +QML_IMPORT_NAME = Shared +QML_IMPORT_MAJOR_VERSION = 1 + HEADERS += \ connectionhandler.h \ deviceinfo.h \ diff --git a/examples/bluetooth/heartrate-game/main.cpp b/examples/bluetooth/heartrate-game/main.cpp index 9722a82f..5e853a4f 100644 --- a/examples/bluetooth/heartrate-game/main.cpp +++ b/examples/bluetooth/heartrate-game/main.cpp @@ -44,12 +44,12 @@ int main(int argc, char *argv[]) DeviceHandler deviceHandler; DeviceFinder deviceFinder(&deviceHandler); - qmlRegisterUncreatableType("Shared", 1, 0, "AddressType", "Enum is not a type"); - QQmlApplicationEngine engine; - engine.rootContext()->setContextProperty("connectionHandler", &connectionHandler); - engine.rootContext()->setContextProperty("deviceFinder", &deviceFinder); - engine.rootContext()->setContextProperty("deviceHandler", &deviceHandler); + engine.setInitialProperties({ + {"connectionHandler", QVariant::fromValue(&connectionHandler)}, + {"deviceFinder", QVariant::fromValue(&deviceFinder)}, + {"deviceHandler", QVariant::fromValue(&deviceHandler)} + }); engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); if (engine.rootObjects().isEmpty()) diff --git a/examples/bluetooth/heartrate-game/qml/main.qml b/examples/bluetooth/heartrate-game/qml/main.qml index 8eb26247..44d824fa 100644 --- a/examples/bluetooth/heartrate-game/qml/main.qml +++ b/examples/bluetooth/heartrate-game/qml/main.qml @@ -4,6 +4,7 @@ import QtQuick import QtQuick.Window import "." +import Shared Window { id: wroot @@ -13,6 +14,10 @@ Window { title: qsTr("HeartRateGame") color: GameSettings.backgroundColor + required property ConnectionHandler connectionHandler + required property DeviceFinder deviceFinder + required property AddressType deviceHandler + Component.onCompleted: { GameSettings.wWidth = Qt.binding(function() {return width}) GameSettings.wHeight = Qt.binding(function() {return height}) -- cgit v1.2.1