Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 00:31, 23 February 2012 by hamishwillee (Talk | contribs)

Implementing custom orientation changes animation with QML

From Wiki
Jump to: navigation, search

Tip.pngTip: This entry has been proposed as a candidate Knowledge Base Developer Contributed article.

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: hamishwillee (05 Oct 2011)
This article pre-dates Qt Quick Components and should be re-written. Note that some of the information in this topic may still be useful.

The right way to do this is to use the Window component from the new Qt Quick Components. If you use Window as the parent element, you can query the inPortrait property to get the orientation. It handles the E7 keyboard open -scenario correctly.

Documentation: Taking Qt Quick components into use:

This text was taken from Comment by korva 13:02, 29 September 2011 (EEST)

Article Metadata
Code ExampleTested with
Devices(s): Nokia E7, Nokia N8, Nokia N900
Platform(s): Maemo, Symbian
Keywords: QML, Orientation, Qt Mobility
Created: kratsan (17 Nov 2010)
Last edited: hamishwillee (23 Feb 2012)



This snippet shows how to implement nice custom orientation change animation with QML.

There are two ways to detect the orientation of the device:

  • by observing the width and height of the main window and seeing which one is the greatest (works if the orientation is not locked) or
  • by using the device's sensors to detect the orientation

In this snippet we use the latter technique and utilize Qt Mobility QOrientationSensor to access the accelerometer data of the device.

To prevent devices built-in orientation change to occur we lock the orientation to portrait mode and simulate the orientation change in QML code. This concerns only Symbian devices as in Maemo the orientation is already locked to the landscape mode. The orientation data is provided to QML code via Qt Signals and Slots.

Pros and cons of this approach:

+ Nice animation to the orientation change
+ No black screen during the orientation change
- Custom implementation is required to switch to landscape orientation when HW-keyboard is used
- Additional (though user-grantable) capability ReadDeviceData is required to allow reading of sensors on Symbian
- Qt Mobility libraries are required to detect the orientation

Note.pngNote: This snippet is valid for Symbian^3 and Symbian Anna. In Symbian Belle there will be a nice orientation change animation working out-of-the-box.

Note.pngNote: This snippet is likely to be deprecated when the Qt Mobility 1.1 will be available in Nokia Qt SDK. The orientation information can then be accessed directly via appropriate QML elements and the Qt code in this snippet will not be required, although it should still be valid code.

Download the sources of this snippet here.


  • Qt 4.7 or higher is installed on your platform
  • QtMobility 1.0.2 or higher is installed on your platform

The declarative module is used, as well as the Qt Mobility sensors. QML code will be placed inside Qt resource system for easier deployment to the devices.

QT          += core gui declarative
TARGET = orientation
SOURCES += main.cpp
HEADERS += orientationfilter.h
OTHER_FILES += Orientation.qml
RESOURCES = resources.qrc
CONFIG += mobility
MOBILITY += sensors
symbian {
# To lock the application to landscape orientation
LIBS += -lcone -leikcore -lavkon
unix:!symbian {
maemo5 {
target.path = /opt/usr/bin
} else {
target.path = /usr/local/bin
INSTALLS += target


The main.cpp creates the QOrientationSensor and the self-implemented OrientationFilter which will signal the changes of the orientation. This signal is connected to the QML documents root elements function orientationChanged. The root element is retrieved by using code view.rootObject().

The width and height of the root element are set to be resized to QDeclarativeViews sizes with the code view.setResizeMode(QDeclarativeView::SizeRootObjectToView).

#include <QApplication>
#include <QDeclarativeView>
#include <QGraphicsObject>
#include <QDesktopWidget>
#include <QOrientationSensor>
#include "orientationfilter.h"
// Lock orientation in Symbian
#include <eikenv.h>
#include <eikappui.h>
#include <aknenv.h>
#include <aknappui.h>
int main(int argc, char *argv[])
QApplication app(argc, argv);
// Lock orientation in Symbian
CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
TRAP_IGNORE( if(appUi) { appUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait); } );
QDeclarativeView view;
QOrientationSensor sensor;
OrientationFilter filter;
// Connect the Qt signal to QML function
QObject::connect(&filter, SIGNAL(orientationChanged(const QVariant&)), view.rootObject(), SLOT(orientationChanged(const QVariant&)));
// Show application in full screen on all platforms with corresponding device resolution
return app.exec();


The self-implemented OrientationFilter] is derived from QObject to get the possibility to emit signals and from QOrientationFilter to read the values of the sensor. The sensor reading is placed to QVariant so that the value can be passed to JavaScript function in QML code.

#include <QOrientationFilter>
class OrientationFilter : public QObject, public QOrientationFilter
bool filter(QOrientationReading *reading) {
emit orientationChanged(reading->orientation());
// don't store the reading in the sensor
return false;
void orientationChanged(const QVariant &orientation);


The UI holds six Text elements inside a Column representing orientations of the device. Current orientation text will be shown as bold and white color as the others are shown in black without bold. The view will be rotated to the current orientation by altering the rotation property of the view. Also the width and height of the view are updated to match the corresponding orientation.

import Qt 4.7
Item {
id: base
function orientationChanged(orientation) {
if(orientation == 1) {
view.rotation = 0
view.width = base.width; view.height = base.height
else if(orientation == 2) {
view.rotation = 180
view.width = base.width; view.height = base.height
else if(orientation == 3) {
view.rotation = 270
view.width = base.height; view.height = base.width
else if(orientation == 4) {
view.rotation = 90
view.width = base.height; view.height = base.width
function highlightindex(orientation) {
var count = texts.children.length
for(var i=0; i<count; i++) {
if(i == (orientation - 1)) {
texts.children[i].color = "white"
texts.children[i].font.bold = true
else {
texts.children[i].color = "black"
texts.children[i].font.bold = false
Rectangle {
id: view
Behavior on rotation { RotationAnimation { direction: RotationAnimation.Shortest; duration: 500; easing.type: Easing.OutBounce } }
Behavior on width { NumberAnimation { duration: 500 } }
Behavior on height { NumberAnimation { duration: 500 } }
anchors.centerIn: parent
width: base.width; height: base.height
color: "blue"
Column {
id: texts
anchors.centerIn: parent
spacing: 10
Text { text: "Top up" }
Text { text: "Top Down" }
Text { text: "Left Up" }
Text { text: "Right Up" }
Text { text: "Face Up" }
Text { text: "Face Down" }


The snippet demonstrated the implementing of nice animation to orientation change with QML language. The orientation data was provided by Qt Mobilitys QRotationSensor class. The retrieved data was transferred via Qt Signals and Slots to QML code where the current orientation was nicely visualized.

105 page views in the last 30 days.