summaryrefslogtreecommitdiffstats
path: root/examples/sensors
diff options
context:
space:
mode:
authorTamas Martinec <tamas.martinec@symbio.com>2021-06-10 13:14:04 +0300
committerAlexandru Croitor <alexandru.croitor@qt.io>2021-08-18 15:15:02 +0200
commita063b9f61ddea7403d36a5288ff6f8dc0d95ddfb (patch)
treef8b116d977bf3774c5c9c743a253106879f67a19 /examples/sensors
parent63fe6819c79c835adf652f8dd99fda54f6d35cdc (diff)
QtSensors: Fix and simplify the Grue example
Refactoring to use QML cpp class registration instead of a QML plugin. Pick-to: 6.2 Task-number: QTBUG-92514 Change-Id: I7637232d27575522e83ebdbc5c60cdb5663d2cd0 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io> Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Diffstat (limited to 'examples/sensors')
-rw-r--r--examples/sensors/CMakeLists.txt3
-rw-r--r--examples/sensors/grue/CMakeLists.txt60
-rw-r--r--examples/sensors/grue/console_app/CMakeLists.txt13
-rw-r--r--examples/sensors/grue/console_app/console_app.pro3
-rw-r--r--examples/sensors/grue/doc/src/grue.qdoc68
-rw-r--r--examples/sensors/grue/grue.pro12
-rw-r--r--examples/sensors/grue/grue.qml15
-rw-r--r--examples/sensors/grue/grue_plugin_import_custom.cpp52
-rw-r--r--examples/sensors/grue/import/import.json1
-rw-r--r--examples/sensors/grue/import/import.pro38
-rw-r--r--examples/sensors/grue/import/main.cpp159
-rw-r--r--examples/sensors/grue/import/qmldir2
-rw-r--r--examples/sensors/grue/lib/CMakeLists.txt38
-rw-r--r--examples/sensors/grue/lib/lib.pri7
-rw-r--r--examples/sensors/grue/lib/lib.pro21
-rw-r--r--examples/sensors/grue/main.cpp13
-rw-r--r--examples/sensors/grue/plugin/CMakeLists.txt40
-rw-r--r--examples/sensors/grue/plugin/gruesensor.cpp (renamed from examples/sensors/grue/lib/gruesensor.cpp)0
-rw-r--r--examples/sensors/grue/plugin/gruesensor.h (renamed from examples/sensors/grue/lib/gruesensor.h)12
-rw-r--r--examples/sensors/grue/plugin/gruesensor_p.h (renamed from examples/sensors/grue/lib/gruesensor_p.h)0
-rw-r--r--examples/sensors/grue/plugin/main.cpp1
-rw-r--r--examples/sensors/grue/plugin/plugin.pro17
-rw-r--r--examples/sensors/grue/qml.pro22
-rw-r--r--examples/sensors/grue/qml.qrc2
-rw-r--r--examples/sensors/grue/qmlgruesensor.cpp58
-rw-r--r--examples/sensors/grue/qmlgruesensor.h66
-rw-r--r--examples/sensors/grue/use_grue_static_plugin.pri17
-rw-r--r--examples/sensors/stub.h1
28 files changed, 355 insertions, 386 deletions
diff --git a/examples/sensors/CMakeLists.txt b/examples/sensors/CMakeLists.txt
index cb66e663..329ae504 100644
--- a/examples/sensors/CMakeLists.txt
+++ b/examples/sensors/CMakeLists.txt
@@ -1,6 +1,5 @@
-# TODO Qt 6.2 enable
-# add_subdirectory(grue)
if(TARGET Qt::Quick)
+ add_subdirectory(grue)
add_subdirectory(maze)
# add_subdirectory(qmlsensorgestures)
add_subdirectory(qmlqtsensors)
diff --git a/examples/sensors/grue/CMakeLists.txt b/examples/sensors/grue/CMakeLists.txt
index 043330d3..e1a962fe 100644
--- a/examples/sensors/grue/CMakeLists.txt
+++ b/examples/sensors/grue/CMakeLists.txt
@@ -13,44 +13,56 @@ endif()
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sensors/grue")
-find_package(Qt6 COMPONENTS Quick)
+find_package(Qt6 COMPONENTS Quick Sensors)
qt_add_executable(grue_app
- main.cpp
+ main.cpp qmlgruesensor.h qmlgruesensor.cpp
)
set_target_properties(grue_app PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)
-if(TARGET Qt::Quick)
- target_link_libraries(grue_app PUBLIC
- Qt::Quick
- )
-endif()
-
-# Resources:
-set(qml_resource_files
- "grue.png"
- "grue.qml"
+target_link_libraries(grue_app PUBLIC
+ Qt::Quick
+ Qt::Sensors
)
-qt6_add_resources(grue_app "qml"
- PREFIX
- "/"
- FILES
- ${qml_resource_files}
+qt6_add_qml_module(grue_app
+ VERSION 1.0
+ URI "QMLGrueSensor"
+ QML_FILES
+ grue.qml
+ RESOURCES
+ grue.png
)
+add_subdirectory(plugin)
+
+# Need to link to the plugin manually in a static Qt build.
+if(NOT QT6_IS_SHARED_LIBS_BUILD)
+ target_link_libraries(grue_app PRIVATE qtsensors_grue)
+ target_sources(grue_app PRIVATE grue_plugin_import_custom.cpp)
+endif()
+
+set(build_console_app TRUE)
+
+# The console app is not a macos bundle, so the shared library plugin wouldn't be found
+if(APPLE AND QT6_IS_SHARED_LIBS_BUILD)
+ set(build_console_app FALSE)
+endif()
+
+# Gui-less apps don't make sense for these platforms
+if(IOS OR EMSCRIPTEN OR ANDROID)
+ set(build_console_app FALSE)
+endif()
+
+if(build_console_app)
+ add_subdirectory(console_app)
+endif()
+
install(TARGETS grue_app
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)
-
-add_subdirectory(lib)
-add_subdirectory(plugin)
-add_subdirectory(console_app)
-if(TARGET Qt::Quick)
- add_subdirectory(import)
-endif()
diff --git a/examples/sensors/grue/console_app/CMakeLists.txt b/examples/sensors/grue/console_app/CMakeLists.txt
index e6364e48..69a3f68c 100644
--- a/examples/sensors/grue/console_app/CMakeLists.txt
+++ b/examples/sensors/grue/console_app/CMakeLists.txt
@@ -24,11 +24,24 @@ set_target_properties(detect_grue PROPERTIES
WIN32_EXECUTABLE FALSE
MACOSX_BUNDLE FALSE
)
+
+if(WIN32)
+ set_target_properties(detect_grue PROPERTIES
+ RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/..
+ )
+endif()
+
target_link_libraries(detect_grue PUBLIC
Qt::Core
Qt::Sensors
)
+# Need to link to the plugin manually in a static Qt build.
+if(NOT QT6_IS_SHARED_LIBS_BUILD)
+ target_link_libraries(detect_grue PRIVATE qtsensors_grue)
+ target_sources(detect_grue PRIVATE ../grue_plugin_import_custom.cpp)
+endif()
+
install(TARGETS detect_grue
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
diff --git a/examples/sensors/grue/console_app/console_app.pro b/examples/sensors/grue/console_app/console_app.pro
index 7b082d39..97021f1d 100644
--- a/examples/sensors/grue/console_app/console_app.pro
+++ b/examples/sensors/grue/console_app/console_app.pro
@@ -8,6 +8,9 @@ DESTDIR = $$OUT_PWD/..
SOURCES = main.cpp
+grue_plugin_base_dir = ..
+include(../use_grue_static_plugin.pri)
+
target.path = $$[QT_INSTALL_EXAMPLES]/sensors/grue
INSTALLS += target
diff --git a/examples/sensors/grue/doc/src/grue.qdoc b/examples/sensors/grue/doc/src/grue.qdoc
index 146a8de0..63f3ba92 100644
--- a/examples/sensors/grue/doc/src/grue.qdoc
+++ b/examples/sensors/grue/doc/src/grue.qdoc
@@ -28,48 +28,41 @@
/*!
\example grue
\title Qt Sensors - Grue Sensor Example
- \brief The Qt Sensors - Grue sensor example demonstrates all the steps from creating a new sensor to using it.
+ \brief The Qt Sensors - Grue sensor example demonstrates all the steps from
+ creating a new sensor to using it.
\ingroup qtsensors-examples
\image qtsensors-examples-grue.png
- The Qt Sensors - Grue sensor example demonstrates all the steps from creating a new sensor to using it.
-
- The sensor definition is placed in a library where client apps can access it. The actual implementation
- lives in a plugin.
+ The sensor definition and implementation are in a new sensor plugin that client
+ apps can use for detecting Grues (imaginary monsters that live in the dark).
\list
- \li \l{Grue Sensor Definition}
- \li \l{Grue Sensor Implementation}
+ \li \l{Grue Sensor Plugin}
\endlist
- The sensor can now be used by a C++ application, even if the application does not have access to the
- definition.
+ The sensor plugin can be used by C++ applications as shown in the console
+ application example.
\list
\li \l{Grue Sensor Console Application}
\endlist
- To make the sensor available to a QML application an import must be created.
+ QML applications can use the new sensor by importing the QMLGrueSensor class.
\list
- \li \l{Grue Sensor QML Import}
\li \l{Grue Sensor QML Application}
\endlist
- \section1 Grue Sensor Definition
-
- The Grue sensor is defined in a library so that applications can use it.
- The source code is available in the \c{grue/lib} subdirectory.
+ \section1 Grue Sensor Plugin
- First up is the sensor type. This is the interface for sensors that report
- on your likelihood of being eaten by a Grue. Such sensors are very important
- to adventurers, particularly if they are going into dark places as this is
- where Grues live.
+ The Grue sensor is defined in a new sensor plugin that applications can use.
- The interface is a simple one. It provides only 1 piece of information, your
- chance of being eaten. For the details on how this is property should be
- interpreted please see the documentation in gruesensor.cpp.
+ The plugin provides the sensor reading property that describes your chance of
+ being eaten. This chance is increasing in the dark until it is 100% when
+ you are eaten by the Grue and at that point the plugin stops further processing.
+ In case of the plugin receiving light again before that happens the chance of
+ being eaten resets to 0%.
This example was created using the make_sensor.pl script which can be found in
src/sensors. As such, it contains some generated code that defines the convenience
@@ -77,8 +70,8 @@
\section1 Grue Sensor Implementation
- The Grue sensor implementation lives in a plugin that is loaded by the Qt Sensors
- library. The source code is available in the \c{grue/plugin} subdirectory.
+ The Grue sensor implementation lives in the plugin that is loaded by the Qt
+ Sensors library. The source code is available in the \c{grue/plugin} subdirectory.
The Grue sensor needs a backend before it can be used. The backend provided
is rather basic and it relies on some kind of light sensor to work but it
@@ -89,8 +82,8 @@
There are a few mandatory parts to a backend. They are the start and stop methods
and the setReading call. The start and stop methods are used to start and stop
any underlying hardware. In the case of this backend they start and stop a
- light sensor. In the start method, the backend should be sure to call the
- sensorStopped() or sensorBusy() methods if it cannot start.
+ light sensor. In the start method, the backend should call the \c{sensorStopped()}
+ or \c{sensorBusy()} methods if it cannot start.
\snippet grue/plugin/gruesensorimpl.cpp start
@@ -111,7 +104,7 @@
The Grue sensor backend also supplies some metadata.
The backend checks 2 things, how dark it is and how long you have been in the dark.
- It uses the readingChanged() signal to know when to check the light sensor's
+ It uses the \c{readingChanged()} signal to know when to check the light sensor's
value. Once it is dark, it uses a timer to increase your chance of being eaten.
The Grue sensor backend is delivered as a plugin. The plugin has a factory object
@@ -123,31 +116,22 @@
The source code is available in the \c{grue/console_app} subdirectory.
This is a simple commandline application. It demonstrates how to use the generic
- access feature of Qt Sensors to avoid a link-time dependency on the Grue Sensor
- library.
-
- \section1 Grue Sensor QML Import
+ access feature of Qt Sensors to avoid a link-time dependency on a library.
- The Grue sensor QML import exports the GrueSensor class as a QML type.
- The source code is available in the \c{grue/import} subdirectory.
+ \section1 Grue Sensor C++ Class Registration for QML
- This creates the \e {Grue 1.0} import.
+ The QMLGrueSensor class is registered for QML, so the class name can be used
+ as an import and the class properties can be accessed from QML.
\section1 Grue Sensor QML Application
- The Grue sensor QML application demonstrates the use of GrueSensor QML type.
+ The Grue sensor QML application demonstrates the use of QMLGrueSensor QML type.
The application consists of a single QML file and an image. It is built as an
- exucutable with C++ code that runs the QML, but it can also be launched directly
+ executable with C++ code that runs the QML, but it can also be launched directly
using the \c qmlscene tool.
- You should build the top-level 'grue' project before trying to run
- this example or it will not be able to find its dependencies.
-
\code
qmlscene -I . grue.qml
\endcode
-
- Above, the \c{-I .} parameter adds the current directory as a module import
- path to locate the Grue QML module.
*/
diff --git a/examples/sensors/grue/grue.pro b/examples/sensors/grue/grue.pro
index 7290cd34..cd23d1f4 100644
--- a/examples/sensors/grue/grue.pro
+++ b/examples/sensors/grue/grue.pro
@@ -1,9 +1,13 @@
TEMPLATE = subdirs
-SUBDIRS += lib plugin console_app
+SUBDIRS += plugin
-qtHaveModule(quick): SUBDIRS += import qml.pro
+# Gui-less apps don't make sense for these platforms
+!uikit:!android:!wasm: SUBDIRS += console_app
+
+# There console app is not a macos bundle, so the shared library plugin wouldn't be found
+macos:qtConfig(shared): SUBDIRS -= console_app
+
+qtHaveModule(quick): SUBDIRS += qml.pro
-plugin.depends = lib
-import.depends = lib
diff --git a/examples/sensors/grue/grue.qml b/examples/sensors/grue/grue.qml
index faaed34f..d02a79ac 100644
--- a/examples/sensors/grue/grue.qml
+++ b/examples/sensors/grue/grue.qml
@@ -50,15 +50,15 @@
import QtQuick
import QtSensors
-import Grue
+import QtQuick.Layouts
+import QMLGrueSensor
Rectangle {
id: root
- width: 320
- height: 480
+ anchors.fill: parent
color: "black"
- property int percent: 0
+ property int percent: -1
property string text: ""
property real grueOpacity: 0.0
@@ -86,7 +86,7 @@ Rectangle {
}
}
- GrueSensor {
+ QMLGrueSensor {
id: sensor
active: true
onReadingChanged: {
@@ -108,10 +108,7 @@ Rectangle {
Text {
id: text
- anchors.top: parent.top
- anchors.topMargin: 0
- anchors.left: parent.left
- anchors.right: parent.right
+ anchors.fill: parent
wrapMode: Text.WordWrap
text: "I can't tell if you're going to be eaten by a Grue or not. You're on your own!"
font.pixelSize: 30
diff --git a/examples/sensors/grue/grue_plugin_import_custom.cpp b/examples/sensors/grue/grue_plugin_import_custom.cpp
new file mode 100644
index 00000000..25f4a61f
--- /dev/null
+++ b/examples/sensors/grue/grue_plugin_import_custom.cpp
@@ -0,0 +1,52 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtPlugin>
+Q_IMPORT_PLUGIN(GrueSensorPlugin)
diff --git a/examples/sensors/grue/import/import.json b/examples/sensors/grue/import/import.json
deleted file mode 100644
index 0967ef42..00000000
--- a/examples/sensors/grue/import/import.json
+++ /dev/null
@@ -1 +0,0 @@
-{}
diff --git a/examples/sensors/grue/import/import.pro b/examples/sensors/grue/import/import.pro
deleted file mode 100644
index f9d5108d..00000000
--- a/examples/sensors/grue/import/import.pro
+++ /dev/null
@@ -1,38 +0,0 @@
-TEMPLATE = lib
-CONFIG += plugin
-QML_IMPORT_VERSION = $$QT_VERSION
-
-TARGET = $$qtLibraryTarget(declarative_grue)
-
-macos: DESTDIR = ../grue_app.app/Contents/MacOS/Grue
-else: DESTDIR = ../Grue
-
-QT = core gui qml sensors
-
-include(../lib/lib.pri)
-
-# Shared gruesensor library will be installed in parent directory.
-# Define rpath so that this plugin will know where to look for it.
-unix:!mac: QMAKE_LFLAGS += -Wl,-rpath,\\\$\$ORIGIN/..
-macos: QMAKE_RPATHDIR += @loader_path/../../Frameworks
-
-SOURCES = main.cpp
-
-DESTPATH=$$[QT_INSTALL_EXAMPLES]/sensors/grue/Grue
-
-target.path=$$DESTPATH
-INSTALLS += target
-
-CONFIG += install_ok # Do not cargo-cult this!
-
-qmldir.files=$$PWD/qmldir
-qmldir.path=$$DESTPATH
-INSTALLS += qmldir
-
-OTHER_FILES += \
- import.json qmldir
-
-# Copy the qmldir file to the same folder as the plugin binary
-cpqmldir.files = $$PWD/qmldir
-cpqmldir.path = $$DESTDIR
-COPIES += cpqmldir
diff --git a/examples/sensors/grue/import/main.cpp b/examples/sensors/grue/import/main.cpp
deleted file mode 100644
index 4827af48..00000000
--- a/examples/sensors/grue/import/main.cpp
+++ /dev/null
@@ -1,159 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2017 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtSensors module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include <QtQml/qqmlextensionplugin.h>
-#include <QtQml/qqml.h>
-
-#include <gruesensor.h>
-#include <QDebug>
-
-#ifdef BUNDLED_PLUGIN
-#include <QPluginLoader>
-#include <QSensorPluginInterface>
-#endif
-
-QT_BEGIN_NAMESPACE
-
-class GrueSensorQmlImport : public QQmlExtensionPlugin
-{
- Q_OBJECT
- Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid FILE "import.json")
-public:
- void registerTypes(const char *uri) override
- {
- char const * const package = "Grue";
- if (QLatin1String(uri) != QLatin1String(package)) return;
- int major;
- int minor;
-
- // Register the 1.0 interfaces
- major = 1;
- minor = 0;
- // @uri Grue
- qmlRegisterType <GrueSensor >(package, major, minor, "GrueSensor");
- qmlRegisterUncreatableType<GrueSensorReading>(package, major, minor, "GrueSensorReading", QLatin1String("Cannot create GrueSensorReading"));
- }
-
-#ifdef BUNDLED_PLUGIN
- GrueSensorQmlImport()
- {
- // For now, this is getting called after Sensors has loaded
- // Ensure that a change later does not break this by forcing
- // sensors to load now
- (void)QSensor::sensorTypes();
-
- // Load the bundled sensor plugin
- QPluginLoader loader(QString::fromLocal8Bit(BUNDLED_PLUGIN));
- QObject *instance = loader.instance();
- m_changes = qobject_cast<QSensorChangesInterface*>(instance);
- if (m_changes) {
- QSensor *sensor = new QSensor(QByteArray(), this);
- connect(sensor, SIGNAL(availableSensorsChanged()), this, SLOT(sensorsChanged()));
- m_changes->sensorsChanged();
- }
- QSensorPluginInterface *plugin = qobject_cast<QSensorPluginInterface*>(instance);
- if (plugin) {
- plugin->registerSensors();
- }
- }
-
-private slots:
- void sensorsChanged()
- {
- m_changes->sensorsChanged();
- }
-
-private:
- QSensorChangesInterface *m_changes;
-#endif
-};
-
-QT_END_NAMESPACE
-
-#include "main.moc"
-
-/*
- \omit
- \qmltype GrueSensor
- \instantiates GrueSensor
- \inherits Sensor
- \inqmlmodule Grue
- \brief The GrueSensor type reports on your chance of being eaten by a Grue.
-
- The GrueSensor type reports on your chance of being eaten by a Grue.
-
- This type wraps the GrueSensor class. Please see the documentation for
- GrueSensor for details.
- \endomit
-*/
-
-/*
- \omit
- \qmltype GrueSensorReading
- \instantiates GrueSensorReading
- \inherits SensorReading
- \inqmlmodule Grue
- \brief The GrueSensorReading type holds the most recent GrueSensor reading.
-
- The GrueSensorReading type holds the most recent GrueSensor reading.
-
- This type wraps the GrueSensorReading class. Please see the documentation for
- GrueSensorReading for details.
-
- This type cannot be directly created.
- \endomit
-*/
-
-/*
- \omit
- \qmlproperty qreal Grue1::GrueSensorReading::chanceOfBeingEaten
- Please see GrueSensorReading::chanceOfBeingEaten for information about this property.
- \endomit
-*/
diff --git a/examples/sensors/grue/import/qmldir b/examples/sensors/grue/import/qmldir
deleted file mode 100644
index 529b9093..00000000
--- a/examples/sensors/grue/import/qmldir
+++ /dev/null
@@ -1,2 +0,0 @@
-module Grue
-plugin declarative_grue
diff --git a/examples/sensors/grue/lib/CMakeLists.txt b/examples/sensors/grue/lib/CMakeLists.txt
deleted file mode 100644
index 264ffd32..00000000
--- a/examples/sensors/grue/lib/CMakeLists.txt
+++ /dev/null
@@ -1,38 +0,0 @@
-cmake_minimum_required(VERSION 3.14)
-project(gruesensor LANGUAGES CXX)
-
-set(CMAKE_INCLUDE_CURRENT_DIR ON)
-
-set(CMAKE_AUTOMOC ON)
-set(CMAKE_AUTORCC ON)
-set(CMAKE_AUTOUIC ON)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sensors/grue")
-
-find_package(Qt6 COMPONENTS Core Sensors)
-
-qt_add_executable(gruesensor
- gruesensor.cpp gruesensor.h gruesensor_p.h
-)
-set_target_properties(gruesensor PROPERTIES
- WIN32_EXECUTABLE TRUE
- MACOSX_BUNDLE TRUE
-)
-target_compile_definitions(gruesensor PUBLIC
- QT_BUILD_GRUE_LIB
-)
-
-target_link_libraries(gruesensor PUBLIC
- Qt::Core
- Qt::Sensors
-)
-
-install(TARGETS gruesensor
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
-)
diff --git a/examples/sensors/grue/lib/lib.pri b/examples/sensors/grue/lib/lib.pri
deleted file mode 100644
index 675914bc..00000000
--- a/examples/sensors/grue/lib/lib.pri
+++ /dev/null
@@ -1,7 +0,0 @@
-INCLUDEPATH += $$PWD
-
-macos: LIBS += -L$$OUT_PWD/../grue_app.app/Contents/Frameworks
-else: LIBS += -L$$OUT_PWD/..
-
-android: LIBS += -lgruesensor_$${QT_ARCH}
-else: LIBS += -lgruesensor
diff --git a/examples/sensors/grue/lib/lib.pro b/examples/sensors/grue/lib/lib.pro
deleted file mode 100644
index 197d91c8..00000000
--- a/examples/sensors/grue/lib/lib.pro
+++ /dev/null
@@ -1,21 +0,0 @@
-TEMPLATE = lib
-TARGET = gruesensor
-android: TARGET = gruesensor_$${QT_ARCH}
-
-macos: DESTDIR = ../grue_app.app/Contents/Frameworks
-else: DESTDIR = $$OUT_PWD/..
-
-macos: QMAKE_SONAME_PREFIX = @rpath
-
-DEFINES *= QT_BUILD_GRUE_LIB
-QT = core sensors
-
-HEADERS += gruesensor.h \
- gruesensor_p.h
-
-SOURCES += gruesensor.cpp
-
-target.path=$$[QT_INSTALL_EXAMPLES]/sensors/grue
-INSTALLS += target
-
-CONFIG += install_ok # Do not cargo-cult this!
diff --git a/examples/sensors/grue/main.cpp b/examples/sensors/grue/main.cpp
index 499a3432..c80e7ef2 100644
--- a/examples/sensors/grue/main.cpp
+++ b/examples/sensors/grue/main.cpp
@@ -48,5 +48,14 @@
**
****************************************************************************/
-#include "../stub.h"
-SENSORS_EXAMPLE_MAIN(grue)
+#include <QGuiApplication>
+#include <QQuickView>
+
+int main(int argc, char **argv)
+{
+ QGuiApplication app(argc,argv);
+ QQuickView view;
+ view.setSource(QUrl("qrc:/QMLGrueSensor/grue.qml"));
+ view.show();
+ return app.exec();
+}
diff --git a/examples/sensors/grue/plugin/CMakeLists.txt b/examples/sensors/grue/plugin/CMakeLists.txt
index d55cc2dc..e8a016e6 100644
--- a/examples/sensors/grue/plugin/CMakeLists.txt
+++ b/examples/sensors/grue/plugin/CMakeLists.txt
@@ -18,35 +18,47 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sensors/grue/sensors")
find_package(Qt6 COMPONENTS Core)
find_package(Qt6 COMPONENTS Sensors)
-qt_add_plugin(qtsensors_grue)
+qt_add_plugin(qtsensors_grue
+ PLUGIN_TYPE sensors
+)
+
target_sources(qtsensors_grue PRIVATE
gruesensorimpl.cpp gruesensorimpl.h
+ gruesensor.cpp gruesensor.h gruesensor_p.h
main.cpp
)
+
set_target_properties(qtsensors_grue PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)
-target_include_directories(qtsensors_grue PUBLIC
- ../lib
-)
+
+if(WIN32)
+ set_target_properties(qtsensors_grue PROPERTIES
+ LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../sensors
+ )
+endif()
+
+if(APPLE AND QT6_IS_SHARED_LIBS_BUILD)
+ set_target_properties(qtsensors_grue PROPERTIES
+ LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../grue_app.app/Contents/MacOS/../sensors
+ )
+endif()
target_link_libraries(qtsensors_grue PUBLIC
Qt::Core
Qt::Sensors
- gruesensor
)
-if(MACOS)
- target_link_libraries(qtsensors_grue PUBLIC
- # Remove: L${CMAKE_CURRENT_BINARY_DIR}/../grue_app.app/Contents/Frameworks
- )
-endif()
+if(ANDROID)
+ add_custom_target(gruesensorplugin_android_copy ALL
+ COMMAND "${CMAKE_COMMAND}"
+ -E copy_if_different
+ "$<TARGET_FILE:qtsensors_grue>"
+ "${CMAKE_CURRENT_BINARY_DIR}/../android-build/libs/${CMAKE_ANDROID_ARCH_ABI}/$<TARGET_FILE_NAME:qtsensors_grue>"
+ COMMENT "Copying sensor plugin lib to grue_app android libs folder.")
-if(NOT MACOS)
- target_link_libraries(qtsensors_grue PUBLIC
- # Remove: L${CMAKE_CURRENT_BINARY_DIR}/..
- )
+ add_dependencies(gruesensorplugin_android_copy qtsensors_grue)
endif()
install(TARGETS qtsensors_grue
diff --git a/examples/sensors/grue/lib/gruesensor.cpp b/examples/sensors/grue/plugin/gruesensor.cpp
index 23eb0ec4..23eb0ec4 100644
--- a/examples/sensors/grue/lib/gruesensor.cpp
+++ b/examples/sensors/grue/plugin/gruesensor.cpp
diff --git a/examples/sensors/grue/lib/gruesensor.h b/examples/sensors/grue/plugin/gruesensor.h
index 31617b8c..c7ec96e3 100644
--- a/examples/sensors/grue/lib/gruesensor.h
+++ b/examples/sensors/grue/plugin/gruesensor.h
@@ -55,13 +55,7 @@
class GrueSensorReadingPrivate;
-#if defined(QT_BUILD_GRUE_LIB)
-# define Q_GRUE_EXPORT Q_DECL_EXPORT
-#else
-# define Q_GRUE_EXPORT Q_DECL_IMPORT
-#endif
-
-class Q_GRUE_EXPORT GrueSensorReading : public QSensorReading
+class GrueSensorReading : public QSensorReading
{
Q_OBJECT
Q_PROPERTY(int chanceOfBeingEaten READ chanceOfBeingEaten WRITE setChanceOfBeingEaten)
@@ -73,7 +67,7 @@ public:
// begin generated code
-class Q_GRUE_EXPORT GrueFilter : public QSensorFilter
+class GrueFilter : public QSensorFilter
{
public:
virtual bool filter(GrueSensorReading *reading) = 0;
@@ -81,7 +75,7 @@ private:
bool filter(QSensorReading *reading) override { return filter(static_cast<GrueSensorReading*>(reading)); }
};
-class Q_GRUE_EXPORT GrueSensor : public QSensor
+class GrueSensor : public QSensor
{
Q_OBJECT
Q_PROPERTY(GrueSensorReading* reading READ reading)
diff --git a/examples/sensors/grue/lib/gruesensor_p.h b/examples/sensors/grue/plugin/gruesensor_p.h
index 0216d06b..0216d06b 100644
--- a/examples/sensors/grue/lib/gruesensor_p.h
+++ b/examples/sensors/grue/plugin/gruesensor_p.h
diff --git a/examples/sensors/grue/plugin/main.cpp b/examples/sensors/grue/plugin/main.cpp
index 6bf86450..a2900cbb 100644
--- a/examples/sensors/grue/plugin/main.cpp
+++ b/examples/sensors/grue/plugin/main.cpp
@@ -63,7 +63,6 @@ class GrueSensorPlugin : public QObject, public QSensorPluginInterface, public Q
public:
void registerSensors() override
{
- qDebug() << "loaded the grue plugin";
}
void sensorsChanged() override
diff --git a/examples/sensors/grue/plugin/plugin.pro b/examples/sensors/grue/plugin/plugin.pro
index e198140d..aa03d6c9 100644
--- a/examples/sensors/grue/plugin/plugin.pro
+++ b/examples/sensors/grue/plugin/plugin.pro
@@ -1,23 +1,20 @@
TEMPLATE = lib
CONFIG += plugin
-TARGET = $$qtLibraryTarget(qtsensors_grue)
+TARGET = $$qtLibraryTarget(plugins_sensors_qtsensors_grue)
PLUGIN_TYPE = sensors
QT = core sensors
-macos: DESTDIR = ../grue_app.app/Contents/MacOS/$$PLUGIN_TYPE
+macos:!qtConfig(static): DESTDIR = ../grue_app.app/Contents/MacOS/$$PLUGIN_TYPE
else: DESTDIR = ../$$PLUGIN_TYPE
-include(../lib/lib.pri)
-
-# Shared gruesensor library will be installed in parent directory.
-# Define rpath so that this plugin will know where to look for it.
-unix:!mac: QMAKE_LFLAGS += -Wl,-rpath,\\\$\$ORIGIN/..
-
-HEADERS += gruesensorimpl.h
+HEADERS += gruesensorimpl.h \
+ gruesensor.h \
+ gruesensor_p.h
SOURCES += gruesensorimpl.cpp \
- main.cpp
+ main.cpp \
+ gruesensor.cpp
# Install the plugin under Grue example directory
diff --git a/examples/sensors/grue/qml.pro b/examples/sensors/grue/qml.pro
index 3293c4f2..6bce739c 100644
--- a/examples/sensors/grue/qml.pro
+++ b/examples/sensors/grue/qml.pro
@@ -1,11 +1,22 @@
TEMPLATE = app
TARGET = grue_app
-QT += quick
+QT += qml quick sensors
# Avoid going to release/debug subdirectory
win32: DESTDIR = ./
-SOURCES = main.cpp
+SOURCES = main.cpp \
+ qmlgruesensor.cpp
+
+HEADERS += \
+ qmlgruesensor.h
+
+grue_plugin_base_dir = .
+include(use_grue_static_plugin.pri)
+
+CONFIG += qmltypes
+QML_IMPORT_NAME = QMLGrueSensor
+QML_IMPORT_MAJOR_VERSION = 1
RESOURCES += \
qml.qrc
@@ -20,3 +31,10 @@ INSTALLS += target
EXAMPLE_FILES += \
grue.xcf \
icon.xcf
+
+android{
+ CONFIG += file_copies
+ COPIES += androidPluginCopy
+ androidPluginCopy.files = $$OUT_PWD/sensors/libplugins_sensors_qtsensors_grue_armeabi-v7a.so
+ androidPluginCopy.path = android-build/libs/armeabi-v7a
+}
diff --git a/examples/sensors/grue/qml.qrc b/examples/sensors/grue/qml.qrc
index 55b269b9..01f4e2bf 100644
--- a/examples/sensors/grue/qml.qrc
+++ b/examples/sensors/grue/qml.qrc
@@ -1,5 +1,5 @@
<RCC>
- <qresource prefix="/">
+ <qresource prefix="/QMLGrueSensor">
<file>grue.qml</file>
<file>grue.png</file>
</qresource>
diff --git a/examples/sensors/grue/qmlgruesensor.cpp b/examples/sensors/grue/qmlgruesensor.cpp
new file mode 100644
index 00000000..bd9bbc0b
--- /dev/null
+++ b/examples/sensors/grue/qmlgruesensor.cpp
@@ -0,0 +1,58 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtSensors module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qmlgruesensor.h"
+
+
+QMLGrueSensor::QMLGrueSensor():QSensor("GrueSensor") // QLightSensor
+{
+ connectToBackend();
+ start();
+}
diff --git a/examples/sensors/grue/qmlgruesensor.h b/examples/sensors/grue/qmlgruesensor.h
new file mode 100644
index 00000000..68927afb
--- /dev/null
+++ b/examples/sensors/grue/qmlgruesensor.h
@@ -0,0 +1,66 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtSensors module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QMLGRUESENSOR_H
+#define QMLGRUESENSOR_H
+
+#include <QtQml/qqml.h>
+#include <QtSensors/qsensor.h>
+
+class QMLGrueSensor : public QSensor
+{
+ Q_OBJECT
+ QML_ELEMENT
+
+public:
+ QMLGrueSensor();
+};
+
+#endif // QMLGRUESENSOR_H
diff --git a/examples/sensors/grue/use_grue_static_plugin.pri b/examples/sensors/grue/use_grue_static_plugin.pri
new file mode 100644
index 00000000..6ccee1cd
--- /dev/null
+++ b/examples/sensors/grue/use_grue_static_plugin.pri
@@ -0,0 +1,17 @@
+# Need to manually link to the plugin when using a static Qt build.
+qtConfig(static) {
+ SOURCES += $$grue_plugin_base_dir/grue_plugin_import_custom.cpp
+ LIBS += -L$$grue_plugin_base_dir/sensors
+ # For iOS, xcode takes care of the debug suffix
+ macx-xcode {
+ LIBS += -lplugins_sensors_qtsensors_grue$($${QMAKE_XCODE_LIBRARY_SUFFIX_SETTING})
+ # For desktop platforms we need to do it per-platform.
+ } else {
+ lib_to_link = plugins_sensors_qtsensors_grue
+ if(!debug_and_release|build_pass):CONFIG(debug, debug|release) {
+ macos:lib_to_link = $${lib_to_link}_debug
+ win32:lib_to_link = $${lib_to_link}d
+ }
+ LIBS += -l$$lib_to_link
+ }
+}
diff --git a/examples/sensors/stub.h b/examples/sensors/stub.h
index 3f198bb1..44356d64 100644
--- a/examples/sensors/stub.h
+++ b/examples/sensors/stub.h
@@ -50,6 +50,7 @@
#include <QGuiApplication>
#include <QQuickView>
+#include <QLoggingCategory>
#define SENSORS_EXAMPLE_MAIN(NAME) int main(int argc, char **argv) \
{\