summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-08-12 11:40:32 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-08-16 15:53:00 +0000
commit40357f4c3b26ffb0ca0696eefcf0ac8dbfe03889 (patch)
tree8ccb39b9c045b2bd09b38b6f3b03fb4fda1e1f8e
parent69da3068dda618b522e7552dd45bcc38d1dc7339 (diff)
downloadqtconnectivity-40357f4c3b26ffb0ca0696eefcf0ac8dbfe03889.tar.gz
Bluetooth heartrate-game example: Use modern QML registration
Use the modern macros and replace setContextProperty(). Change-Id: If83f8a2dfab13e7c1b3dd18048f633c47709a7b3 Reviewed-by: Juha Vuolle <juha.vuolle@insta.fi> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> (cherry picked from commit 94cf544ab0590b9f38fda8875f678ca7dcdb1a7b) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--examples/bluetooth/heartrate-game/CMakeLists.txt6
-rw-r--r--examples/bluetooth/heartrate-game/connectionhandler.h3
-rw-r--r--examples/bluetooth/heartrate-game/devicefinder.h4
-rw-r--r--examples/bluetooth/heartrate-game/devicehandler.h5
-rw-r--r--examples/bluetooth/heartrate-game/heartrate-game.pro4
-rw-r--r--examples/bluetooth/heartrate-game/main.cpp10
-rw-r--r--examples/bluetooth/heartrate-game/qml/main.qml5
7 files changed, 32 insertions, 5 deletions
diff --git a/examples/bluetooth/heartrate-game/CMakeLists.txt b/examples/bluetooth/heartrate-game/CMakeLists.txt
index 341b7d4d..a5339775 100644
--- a/examples/bluetooth/heartrate-game/CMakeLists.txt
+++ b/examples/bluetooth/heartrate-game/CMakeLists.txt
@@ -34,6 +34,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 055b6428..cae81954 100644
--- a/examples/bluetooth/heartrate-game/connectionhandler.h
+++ b/examples/bluetooth/heartrate-game/connectionhandler.h
@@ -53,6 +53,8 @@
#include <QBluetoothLocalDevice>
+#include <qqml.h>
+
#include <QObject>
class ConnectionHandler : public QObject
@@ -64,6 +66,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 c8c5bd3c..61a95dfe 100644
--- a/examples/bluetooth/heartrate-game/devicefinder.h
+++ b/examples/bluetooth/heartrate-game/devicefinder.h
@@ -56,6 +56,8 @@
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
+#include <qqml.h>
+
#include <QTimer>
#include <QVariant>
@@ -69,6 +71,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 7f97c1b2..d89af219 100644
--- a/examples/bluetooth/heartrate-game/devicehandler.h
+++ b/examples/bluetooth/heartrate-game/devicehandler.h
@@ -56,6 +56,8 @@
#include <QLowEnergyController>
#include <QLowEnergyService>
+#include <qqml.h>
+
#include <QDateTime>
#include <QList>
#include <QTimer>
@@ -76,6 +78,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 d0013c47..df2c247d 100644
--- a/examples/bluetooth/heartrate-game/main.cpp
+++ b/examples/bluetooth/heartrate-game/main.cpp
@@ -91,12 +91,12 @@ int main(int argc, char *argv[])
DeviceHandler deviceHandler;
DeviceFinder deviceFinder(&deviceHandler);
- qmlRegisterUncreatableType<DeviceHandler>("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 45d8073a..4f8d3b41 100644
--- a/examples/bluetooth/heartrate-game/qml/main.qml
+++ b/examples/bluetooth/heartrate-game/qml/main.qml
@@ -51,6 +51,7 @@
import QtQuick
import QtQuick.Window
import "."
+import Shared
Window {
id: wroot
@@ -60,6 +61,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})