Important: Please read the Qt Code of Conduct -

How to use QSensor? reading() returns a nullptr

  • I want to get the some sensor's information of Android device, so I use these class to make it: QGyroscope, QAccelerometer and QRotationSensor. The code is below:

    #include <QGyroscope>
    #include <QAccelerometer>
    #include <QRotationSensor>
    #include <QWidget>
    #include <thread>
    namespace Ui {
    class SensorWidget;
    class SensorWidget : public QWidget
        explicit SensorWidget(QWidget *parent = nullptr);
        void showData();
        Ui::SensorWidget *ui;
        bool m_thread_flag;
        std::thread* m_thread;
        QGyroscope* m_gyroscope;
        QGyroscopeReading* m_gyro_reader;
        QAccelerometer* m_accelerometer;
        QAccelerometerReading* m_acc_reader;
        QRotationSensor* m_rotation_sensor;
        QRotationReading* m_rotation_reader;
    #include "SensorWidget.h"
    #include "ui_SensorWidget.h"
    #include <QDebug>
    SensorWidget::SensorWidget(QWidget *parent) :
                                                    ui(new Ui::SensorWidget),
                                                    m_thread(new std::thread(&SensorWidget::showData, this)),
                                                    m_gyroscope(new QGyroscope(this)),
                                                    m_accelerometer(new QAccelerometer(this)),
                                                    m_rotation_sensor(new QRotationSensor(this)),
        m_thread_flag = false;
        delete ui;
    void SensorWidget::showData()
            m_gyro_reader = m_gyroscope->reading();
            qDebug() << "Phone's X gyroscope = " << (m_gyro_reader->x());
            qDebug() << "Phone's Y gyroscope = " << (m_gyro_reader->y());
            qDebug() << "Phone's Z gyroscope = " << (m_gyro_reader->z());
            m_acc_reader = m_accelerometer->reading();
            qDebug() << "Phone's X accelerometer = " << (m_acc_reader->x());
            qDebug() << "Phone's Y accelerometer = " << (m_acc_reader->y());
            qDebug() << "Phone's Z accelerometer = " << (m_acc_reader->z());
            m_rotation_reader = m_rotation_sensor->reading();
            qDebug() << "Phone's X rotation = " << (m_rotation_reader->x());
            qDebug() << "Phone's Y rotation = " << (m_rotation_reader->y());
            qDebug() << "Phone's Z rotation = " << (m_rotation_reader->z());

    I got an error when run the app on the Android and Windows, then I debug it and found that, m_gyroscope->reading() returns nothing but a nullptr. At last, I found m_gyroscope->start() is failed, it return false.
    Is there any mistake I made in my code. What should I do to get those information?

  • Lifetime Qt Champion


    Have you already had a look at the examples: ?

    Also, it seems you are trying to operate with threads. Don't do that. Qt is asynchronous by nature, so you should use signals&slots extensively.

    Threading is seldom necessary therefore and requires deep understanding of what's going on.


  • @aha_1980 thanks, I tried to move off the thread before, and got the same problem. I will take a good look at examples you give me, thanks very much.

  • The problem has been solved. Firstly, I removed the thread, QSensor can't works with thread.
    And then, in .pro file, add these lines:

    CONFIG += mobility

    In head file, add these lines:

    #include <QGyroscope>
    #include <qgyroscope.h>
    #include <QAccelerometer>
    #include <qaccelerometer.h>
    #include <QRotationSensor>
    #include <qrotationsensor.h>

    In cpp file, use signal&slot instead of thread

    connect(m_gyroscope, SIGNAL(readingChanged()), this, SLOT(onGyroReadingChanged()));
    connect(m_accelerometer, SIGNAL(readingChanged()), this, SLOT(onAccReadingChanged()));
    connect(m_rotation_sensor, SIGNAL(readingChanged()), this, SLOT(onRotationReadingChanged()));

    I haven't yet identified the cause of the problem. I'll update this when I check it out.
    Personal opinion:We don't have to deal the problem by these sensor, only if the Gestures provided by Qt can not meeting your needs. It's too difficult to make a clear sense of these sensors, you may have to research on the knowledge about physics and how it relates to mobile device.

    [Edit aha_1980: fixed typo]

  • Moderators

    @Donation said in How to use QSensor? reading() returns a nullptr:

    CONFIG += mobolity

    that's not going to make much of a difference. I Assume you mean CONFIG += mobility ?

  • @J.Hilk sorry, it's a misspelling. yeah I'm not sure what makes the error now, maybe CONFIG+=mobility makes no difference, it seems would be added by Qt by default when I use Android Kits to build the project. I will check it out later. Thanks your reminding.
    By the way, can you give some suggestions to me, what cause the error do you think? thanks very much.

  • Lifetime Qt Champion

    @Donation said in How to use QSensor? reading() returns a nullptr:

    #include <QGyroscope>
    #include <qgyroscope.h>
    #include <QAccelerometer>
    #include <qaccelerometer.h>
    #include <QRotationSensor>
    #include <qrotationsensor.h>

    I think the following is enough:

    #include <QGyroscope>
    #include <QAccelerometer>
    #include <QRotationSensor>

  • Moderators

    @Donation said in How to use QSensor? reading() returns a nullptr:

    By the way, can you give some suggestions to me, what cause the error do you think? thanks very much.

    you did add QT += sensors in your pro file ?
    the sensors module is only supported for Android, iOS, SailFish and WinRT So building your app on Windows, Linux MacOS is going to be complicated(Windows UWP) or impossible(the rest)

  • @J.Hilk sure I did add QT += sensors in my pro file. And I build my project on Andorid.
    I just don't know why it cause that error at the very beginning, now it runs well.

  • Moderators

    may be a manifest issue ? Sensor access requires special permission on android, IIRC


  • @aha_1980 I have tried, and it does enough. Now I can ruled it out, thanks a million.

  • @J.Hilk I don't know, I am a newcomer to Qt for Android. But all the differences between before and after are showing there, I didn't change any other codes or my device. Haha, I will check it out later when I finish my work today. Very very thanks for your help.

Log in to reply