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.

How to use QShake to detect and manage phone shaking

From Wiki
Jump to: navigation, search
Featured Article
01 Apr

This article explains how to use QShake, a utility class that help you to detect and manage phone shaking. The "shake gesture" can be used for many purposes - for example shake controlled shuffling of gallery or songs, a dice game, or "shaking" files to receive or send them (perhaps using shake direction to determine whether files are pushed or pulled).

Article Metadata
Code Example
Source file: QShake
Installation file: QShakeSample.sis from QShake
Tested with
Devices(s): Nokia C7-00, Nokia N8, Nokia N950
Platform(s): Symbian^3 and later, Qt 4.7 and later, Meego
Keywords: QAccelerometer, QAccelerometerReading, QAccelerometerFilter, phone shake,
Created: galazzo (22 Mar 2012)
Last edited: kiran10182 (30 Oct 2013)


Using QShake

  1. Download source code from here
  2. Include QShake directory into your project

Using QVibra

For instructions on how to use QVibra please refer to How to use QVibra to enable vibration in QML.




#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QtDeclarative>
#include "qshake/qshake.h"
#ifdef QVIBRA
#include "QVibra/qvibra.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
QScopedPointer<QApplication> app(createApplication(argc, argv));
qmlRegisterType<QShake>("QShake", 1, 0, "Shake");
#ifdef QVIBRA
qmlRegisterType<QVibra>("Vibra", 1, 0, "Vibra");
QmlApplicationViewer viewer;
return app->exec();


import QtQuick 1.1
import 1.1
import QShake 1.0
import Vibra 1.0
Page {
id: mainPage
property string romanticPhrase: ""
FontLoader { id: appFont; source: "font/andyb.ttf" }
property variant sentences: [
,"Love can conquer distance, but you have to want it and be willing to wait."
,"True love is saying everything with one look and not one word."
Image {
width: parent.width
height: parent.height
fillMode: Image.TileHorizontally
smooth: true
source: "images/sfondo-pagina.png"
Column {
Row {
Rectangle {
width: 180
height: 320
Image {
width: 180
height: 232
smooth: true
source: "images/button-profile-lui.png"
MouseArea {
anchors.fill: parent
onClicked: window.pageStack.push(Qt.resolvedUrl("ProfileMale.qml"))
Rectangle {
width: 180
height: 320
Image {
height: 232
smooth: true
source: "images/button-profile-lei.png"
MouseArea {
anchors.fill: parent
onClicked: window.pageStack.push(Qt.resolvedUrl("ProfileFemale.qml"))
Rectangle {
width: 180
height: 320
Image {
id: image1
x: 0
y: -90
width: 360
smooth: true
source: "images/sfondo-status.png"
Image {
x: 29
y: 125
width: 128
anchors.horizontalCenterOffset: -87
anchors.horizontalCenter: parent.horizontalCenter
source: "images/tempo/0.png"
Text {
id: tip
x: 210
y: 94
width: 139
height: 257
color: "#ffffff"
text: qsTr("")
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
font.italic: true
font.pixelSize: 22
Vibra {
Shake {
sensitivity: Shake.Medium
direction: Shake.All
onShake: {
if( shaked ){
romanticPhrase = changePhrase();
function changePhrase(){
var now = new Date();
var seed = now.getSeconds();
var num = (Math.floor(100*Math.random(seed)))%30;
return sentences[num+1];
SequentialAnimation {
id: animation
running: false
NumberAnimation { target: tip; property: "opacity"; to: 0.0; duration: 600}
PropertyAction { target:tip; property: "text"; value: romanticPhrase }
NumberAnimation { target: tip; property: "opacity"; to: 1.0; duration: 600}
Component.onCompleted: shake.start();


There are three possible integer values to set sensitivity:

  • High = 40
  • Medium = 30
  • Low = 20

Anyway you can set your preferred value, for example:

Shake {
sensitivity: 36


You can also force direction detection

Shake {
direction: Shake.PushPull

Possible values are:

  • All
  • LeftRight
  • UpDown
  • PushPull

The sensitivity (difference) between Shake.Medium and Shake.High is reduced for values other than All.

Remember that it's a forcing. It works well on Symbian devices, but if you shake too vigorously it is not possible to guarantee the correct direction detection.

I also noticed that there is a small difference between Symbian and Meego devices regarding sensitivity and direction, so it may be you need to change some values.


  • Self-signed QShakeSample.sis can be downloaded from QShake for testing.

Featured Sample

  • A real application AppLove can be downloaded.
  • Shake your phone to discover a new romantic phrase


This page was last modified on 30 October 2013, at 20:15.
77 page views in the last 30 days.