Unsolved How to use QSensor? reading() returns a nullptr
-
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
-
@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]
-
@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. -
@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> -
@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 forAndroid, 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. -
@Donation
may be a manifest issue ? Sensor access requires special permission on android, IIRC
android.hardware.sensor.gyroscope
android.hardware.sensor.accelerometer -
@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.