summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-08-12 11:40:32 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-08-16 14:46:07 +0200
commit94cf544ab0590b9f38fda8875f678ca7dcdb1a7b (patch)
treed31b4523b8033df29821a4b3e01f473ac87a37a8 /examples
parent01a6481998e692e014489b97d295bc8b0f2d10ef (diff)
downloadqtconnectivity-94cf544ab0590b9f38fda8875f678ca7dcdb1a7b.tar.gz
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 <juha.vuolle@insta.fi> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'examples')
-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 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 <QBluetoothLocalDevice>
+#include <qqml.h>
+
#include <QObject>
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 <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
+#include <qqml.h>
+
#include <QTimer>
#include <QVariant>
@@ -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 <QLowEnergyController>
#include <QLowEnergyService>
+#include <qqml.h>
+
#include <QDateTime>
#include <QList>
#include <QTimer>
@@ -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<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 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})