Qt Sensors - C++ Sensor Gestures Example

The sensor gesture example demonstrates the steps for creating a C++ application that uses sensor gestures and the included shake sensor.

QSensorGestures class defines one predefined signal, void detected(const QString &)

As well, Sensor Gesture Recognizers may implement any number of their own custom signals.

Our shake recognizer defines one custom signal, void shake(), as well as the predefined detected signal.

SensorGestures Class Implementation

In order to know about custom signals that may be available, we need to ask the QSensorGestureManager about them,

Using the QSensorGesture::gestureIds() function, the manager will return a QStringList of known and valid gesture Ids.

 QSensorGestureManager manager;

 Q_FOREACH (const QString &gesture, manager.gestureIds()) {

     QTreeWidgetItem *gestureId = new QTreeWidgetItem(ui->treeWidget);
     QStringList recognizerSignals =  manager.recognizerSignals(gesture);

     for (int i = 0; i < recognizerSignals.count(); i++) {
         QTreeWidgetItem *oneSignal = new QTreeWidgetItem(gestureId);

We can then use this to create a QSensorGesture object that we can use to connect signals to, and start the detection process. A QSensorGesture object will take a list of one or more recognizer ids in it's constructor.

 QSensorGestureManager manager;
 QSensorGesture *thisGesture = new QSensorGesture(QStringList() << currentRecognizer, this);

 if (currentRecognizer.contains("QtSensors.shake")) {


and later stop the detection process.


     if (currentRecognizer == "QtSensors.shake") {

The QSensorGesture object will contain all the signals of the valid requested recognizers found on the system.

You can discover which of the requested recognizer ID's that were not found by using QSensorGesture::invalidIds();

By using QSensorGesture::gestureSignals(), you can get a QStringList of usable signals.

Example project @ code.qt.io

© 2024 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.