Reader small image

You're reading from  Application Development with Qt Creator - Third Edition

Product typeBook
Published inJan 2020
Reading LevelBeginner
Publisher
ISBN-139781789951752
Edition3rd Edition
Languages
Right arrow
Author (1)
Lee Zhi Eng
Lee Zhi Eng
author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng

Right arrow

Sensors and Qt Quick

any of today's devices come with a myriad of sensors, including a means to determine the device's position and orientation, as well as to measure the characteristics of its surroundings through thermometers, luminescence sensors, accelerometers, gyroscopes, and other sensors. This is especially true of cell phones and other portable devices. To learn more about all the sensors that are available in our mobile devices, please read the article at: https://gizmodo.com/all-the-sensors-in-your-smartphone-and-how-they-work-1797121002.

In this chapter, we will take a look at Qt's sensor and positioning frameworks since they're supported in QML. You'll learn how to determine a device's position on the surface of the Earth and how to measure the other characteristics of its environment, as reported by its onboard sensors.

In this chapter...

Technical requirements

The technical requirements for this chapter are as follows:

  • Qt 5.12.3 arm64-v8a
  • Qt Creator 4.9.0
  • Windows 10

Accessing sensors in Qt

Qt has had a robust porting layer for device sensors for several years, starting with the Qt Mobility libraries, which were meant to facilitate software development for cell phones. As Qt continued to evolve, support for sensors was added to Qt Quick and the list of supported sensors grew. Today, Qt supports the following sensors:

  • An accelerometer is supported through the Accelerometer type.
  • An altimeter is supported through the Altimeter type.
  • An ambient light sensor is supported through the AmbientLightSensor and LightSensor types.
  • An ambient temperature sensor is supported through the AmbientTemperatureSensor type.
  • A compass is supported through the Compass type.
  • A gyroscope is supported through the Gyroscope type.
  • Whether or not the device is in a holster is determined through the Holster type.
  • Proximity to the device's screen is determined through...

Determining device location

Many devices support position determination, either through hardware such as a Global Positioning System (GPS) receiver or through network resources such as Internet Protocol (IP) geolocation. Similar to the other sensor support, this facility was introduced to Qt in Qt 4.x through the Qt Mobility module and is now supported through the Qt Positioning module. It's supported on many mobile devices, including Android.

To use the Qt Positioning module, you need to include the positioning keyword in your .pro file, as follows:

QT += quick network positioning 

The Qt Positioning module provides three types of positioning; you can access these by importing the QtPositioning module:

  • PositionSource: This provides position updates at a specified rate, emitting the positionChanged signal when position updates are available.
  • Position: In the slot that you...

Obtaining a device's position

Let's move on to a simple example that returns the device's position, accelerometer, gyroscope, ambient light, and magnetometer readings. Here's what our application looks like when running on a Huawei phone:

Note that my Huawei phone does not have a magnetometer, so these readings aren't being updated. Let's see how this works:

  1. First, we need to ensure that we include the positioning and sensor modules in our .pro file (if we don't, the application will compile but fail to launch):
QT += quick positioning sensors 
  1. Next, we'll move on to the QML itself. This is long but straightforward. First, we add the necessary modules to our project; then, we define the Window object for our program:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtPositioning 5.12
import QtSensors 5.12

Window {
visible: true...

Placing a position marker on a Map View

Since version 5.0, Qt provides us with a Map View component that displays a map or image of the Earth, similar to Google Maps. Due to licensing issues, Qt Map View doesn't support Google Maps. The default tiled map service provider for Qt Map View is the community mapping project OpenStreetMap (OSM) since it is free of charge. Other than OSM, you can use other commercial service providers such as Mapbox, ArcGIS, and HERE.

Unlike other third-party mapping solutions, Qt Map View renders the tiled map using a native rendering engine (powered by Qt Quick) instead of embedding a web view onto the app. Native rendering speeds up performance and keeps your app size small since it doesn't include all the unnecessary resources needed by the web view. However, you can't interact with the Map View using C++ at the moment, only QML. I...

Accessing sensors with C++

So far, we have learned how we can access the sensors' data through QML scripting. However, the sensors can also be accessed through Qt's various C++ classes. We will walk through some examples of this and demonstrate how we can achieve that.

First, create an empty Qt project (you can continue from the previous example if you wish, though). Instead of writing the code in the .qml file, we will open up main.cpp instead. After that, we will include some headers so that we can access these classes. Remember to add the sensors module to your project file (.pro) if you are creating a new Qt project:

#include <QDebug>
#include <QTimer>

#include <QAccelerometer>
#include <QAmbientLightSensor>
#include <QProximitySensor>

#include <QAccelerometerReading>
#include <QAmbientLightReading>
#include <QProximityReading...

Summary

In this chapter, you learned how to determine measurements from device sensors, including the device's positioning system, accelerometer, and other sensors. You also learned how to display the device's location on a map so that the user can see the location, along with its context, instead of just coordinating numbers, since positioning helps us trace exact locations.

In the next chapter, we will take a look at how we can use Qt's localization framework and tools to localize our application.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Application Development with Qt Creator - Third Edition
Published in: Jan 2020Publisher: ISBN-13: 9781789951752
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng