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

How to use QSensor? reading() returns a nullptr

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.4k 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.
  • aha_1980A aha_1980

    @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

    DonationD Offline
    DonationD Offline
    Donation
    wrote on 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.

    DonationD 1 Reply Last reply
    0
    • DonationD Donation

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

      DonationD Offline
      DonationD Offline
      Donation
      wrote on 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.HilkJ aha_1980A 2 Replies Last reply
      0
      • DonationD Donation

        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.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on 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.

        DonationD 1 Reply Last reply
        2
        • J.HilkJ J.Hilk

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

          DonationD Offline
          DonationD Offline
          Donation
          wrote on 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.HilkJ 1 Reply Last reply
          0
          • DonationD Donation

            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]

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 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.

            DonationD 1 Reply Last reply
            0
            • DonationD Donation

              @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.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 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.

              DonationD 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

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

                DonationD Offline
                DonationD Offline
                Donation
                wrote on 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.HilkJ 1 Reply Last reply
                0
                • DonationD Donation

                  @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.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on 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.

                  DonationD 1 Reply Last reply
                  1
                  • aha_1980A aha_1980

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

                    DonationD Offline
                    DonationD Offline
                    Donation
                    wrote on 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.HilkJ J.Hilk

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


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

                      DonationD Offline
                      DonationD Offline
                      Donation
                      wrote on 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

                      • Login

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