Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use QSensor? reading() returns a nullptr
Forum Updated to NodeBB v4.3 + New Features

How to use QSensor? reading() returns a nullptr

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Donation
    24 Apr 2019, 06:52

    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:

    //SensorWidget.h
    #include <QGyroscope>
    #include <QAccelerometer>
    #include <QRotationSensor>
    #include <QWidget>
    #include <thread>
    
    namespace Ui {
    class SensorWidget;
    }
    
    class SensorWidget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit SensorWidget(QWidget *parent = nullptr);
        ~SensorWidget();
    
    private:
        void showData();
    
    private:
        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;
    };
    
    //SensorWidget.cpp
    #include "SensorWidget.h"
    #include "ui_SensorWidget.h"
    #include <QDebug>
    
    SensorWidget::SensorWidget(QWidget *parent) :
                                                    QWidget(parent),
                                                    ui(new Ui::SensorWidget),
                                                    m_thread_flag(true),
                                                    m_thread(new std::thread(&SensorWidget::showData, this)),
                                                    m_gyroscope(new QGyroscope(this)),
                                                    m_gyro_reader(nullptr),
                                                    m_accelerometer(new QAccelerometer(this)),
                                                    m_acc_reader(nullptr),
                                                    m_rotation_sensor(new QRotationSensor(this)),
                                                    m_rotation_reader(nullptr)
    {
        ui->setupUi(this);
        m_gyroscope->start();
        m_accelerometer->start();
        m_rotation_sensor->start();
    }
    
    SensorWidget::~SensorWidget()
    {
        m_thread_flag = false;
        m_thread->join();
        delete ui;
    }
    
    void SensorWidget::showData()
    {
        while(m_thread_flag)
        {
            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());
            ui->lineEdit->setText(QString::number(m_gyro_reader->x()));
            ui->lineEdit_2->setText(QString::number(m_gyro_reader->y()));
            ui->lineEdit_3->setText(QString::number(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());
            ui->lineEdit_4->setText(QString::number(m_acc_reader->x()));
            ui->lineEdit_5->setText(QString::number(m_acc_reader->y()));
            ui->lineEdit_6->setText(QString::number(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());
            ui->lineEdit_7->setText(QString::number(m_rotation_reader->x()));
            ui->lineEdit_8->setText(QString::number(m_rotation_reader->y()));
            ui->lineEdit_9->setText(QString::number(m_rotation_reader->z()));
    
            std::this_thread::sleep_for(std::chrono::milliseconds(50));
        }
    }
    

    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?

    A Offline
    A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on 24 Apr 2019, 06:57 last edited by
    #2

    @Donation

    Have you already had a look at the examples: https://doc.qt.io/qt-5/qtsensors-examples.html ?

    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.

    Regards

    Qt has to stay free or it will die.

    D 1 Reply Last reply 24 Apr 2019, 07:10
    1
    • A aha_1980
      24 Apr 2019, 06:57

      @Donation

      Have you already had a look at the examples: https://doc.qt.io/qt-5/qtsensors-examples.html ?

      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.

      Regards

      D Offline
      D Offline
      Donation
      wrote on 24 Apr 2019, 07:10 last edited by
      #3

      @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.

      D 1 Reply Last reply 24 Apr 2019, 09:45
      0
      • D Donation
        24 Apr 2019, 07:10

        @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.

        D Offline
        D Offline
        Donation
        wrote on 24 Apr 2019, 09:45 last edited by aha_1980
        #4

        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]

        J A 2 Replies Last reply 24 Apr 2019, 09:52
        0
        • D Donation
          24 Apr 2019, 09:45

          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]

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 24 Apr 2019, 09:52 last edited by
          #5

          @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 ?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          D 1 Reply Last reply 24 Apr 2019, 11:25
          2
          • J J.Hilk
            24 Apr 2019, 09:52

            @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 ?

            D Offline
            D Offline
            Donation
            wrote on 24 Apr 2019, 11:25 last edited by Donation
            #6

            @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.

            J 1 Reply Last reply 24 Apr 2019, 11:39
            0
            • D Donation
              24 Apr 2019, 09:45

              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]

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 24 Apr 2019, 11:38 last edited by
              #7

              @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>

              Qt has to stay free or it will die.

              D 1 Reply Last reply 24 Apr 2019, 12:30
              0
              • D Donation
                24 Apr 2019, 11:25

                @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.

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 24 Apr 2019, 11:39 last edited by
                #8

                @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)


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                D 1 Reply Last reply 24 Apr 2019, 12:17
                0
                • J J.Hilk
                  24 Apr 2019, 11:39

                  @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)

                  D Offline
                  D Offline
                  Donation
                  wrote on 24 Apr 2019, 12:17 last edited by
                  #9

                  @J.Hilk sure I did add QT += sensors in my pro file. And I build my project on Andorid.
                  0_1556108019921_ee74241b-36aa-4477-98c2-129d337d79d1-image.png
                  0_1556108078410_30f44faa-6eb5-453a-9ed0-e425127c52bb-image.png
                  I just don't know why it cause that error at the very beginning, now it runs well.

                  J 1 Reply Last reply 24 Apr 2019, 12:29
                  0
                  • D Donation
                    24 Apr 2019, 12:17

                    @J.Hilk sure I did add QT += sensors in my pro file. And I build my project on Andorid.
                    0_1556108019921_ee74241b-36aa-4477-98c2-129d337d79d1-image.png
                    0_1556108078410_30f44faa-6eb5-453a-9ed0-e425127c52bb-image.png
                    I just don't know why it cause that error at the very beginning, now it runs well.

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 24 Apr 2019, 12:29 last edited by
                    #10

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


                    android.hardware.sensor.gyroscope
                    android.hardware.sensor.accelerometer


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    D 1 Reply Last reply 24 Apr 2019, 12:45
                    1
                    • A aha_1980
                      24 Apr 2019, 11:38

                      @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>

                      D Offline
                      D Offline
                      Donation
                      wrote on 24 Apr 2019, 12:30 last edited by
                      #11

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

                      1 Reply Last reply
                      0
                      • J J.Hilk
                        24 Apr 2019, 12:29

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


                        android.hardware.sensor.gyroscope
                        android.hardware.sensor.accelerometer

                        D Offline
                        D Offline
                        Donation
                        wrote on 24 Apr 2019, 12:45 last edited by
                        #12

                        @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.

                        1 Reply Last reply
                        0

                        11/12

                        24 Apr 2019, 12:30

                        • Login

                        • Login or register to search.
                        11 out of 12
                        • First post
                          11/12
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved