Qt Sensors - Accel Bubble


Writing a QML application that uses the Accelerometer QML sensors type requires the following steps:

Import the Sensors Declarative module.

  import QtSensors 5.0

Add an Accelerometer QML type.

      Accelerometer {
          id: accel
          dataRate: 100

Use the 'active' property to start the sensor


Move the bubble according to a factor of the accelerator sensor

          onReadingChanged: {
              var newX = (bubble.x + calcRoll(accel.reading.x, accel.reading.y, accel.reading.z) * .1)
              var newY = (bubble.y - calcPitch(accel.reading.x, accel.reading.y, accel.reading.z) * .1)

              if (isNaN(newX) || isNaN(newY))

              if (newX < 0)
                  newX = 0

              if (newX > mainWindow.width - bubble.width)
                  newX = mainWindow.width - bubble.width

              if (newY < 18)
                  newY = 18

              if (newY > mainWindow.height - bubble.height)
                  newY = mainWindow.height - bubble.height

                  bubble.x = newX
                  bubble.y = newY


© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.