Using-QtMobility-sensors-and-QML-from-PySide

From Qt Wiki
Revision as of 18:02, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Using QtMobility sensors and QML from PySide

This PySide tutorial shows how to use QtMobility APIs to read the accelerometer from Python, scale and smoothen the resulting data and expose it to a QML application in order to keep an image always upright. In the future, Qt Mobility 1.2 (still not released as of December 2010) will have QML Plugins [doc.qt.nokia.com], but right now we have to write some glue code in Python (and one might want to do more with the accelerometer data than just using it in the UI layer, so this will still be relevant when Qt Mobility 1.2 is out).

UnderMeSensi.py

Importing the required modules

This is basically the same as in the previous tutorials (PySide modules needed, the QtOpenGL module is optional) with the new addition of the QtMobility Sensors API. On your N900, you have to install the python-qtmobility metapackage in order to get the right modules.

The listener / controller

Next, we need to define a QObject subclass that takes care of receiving events from the accelerometer, scaling and smoothing the value and finally exposing the calculated rotation value as property so that we can access it from within our QML UI:

Putting it all together

We create a new QAccelerometer from the Sensors module, which abstracts away the underlying system accelerometer and sends us easy-to-use events. We then create an instance of our listener class, and connect the readingChanged signal of the accelerometer (which gets called every time the reading changes, obviously) to the listener’s on_reading_changed slot. We also have to tell the accelerometer to start reading the sensor and send out events.

We then only need to set up our QDeclarativeView as usual, and expose our listener object to the QML context, so that we can access it from the UI:

UnderMeSensi.qml

This one is really trivial: Have an enclosing rectangle (which fills the whole screen) and then an image centered into it that shows the PySide logo, gets scaled a bit (so that it fits the screen nicely) and finally has its rotation property set to the rotation property of listener (this is the key part here – it will update the image’s rotation everytime the listener’s rotation property changes).

How the example app looks like

Copy the files UnderMeSensi.py and UnderMeSensi.qml to your N900 and download the file logo.png [pyside.org] as images/pysidelogo.png (or use a custom image of your choosing and set the source: path/URL in the QML file correctly. On an N900, it looks like this:

Example app running on a N900

The example app in action: Video of the example on YouTube [youtu.be]

Categories: