Unsolved 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:
//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? -
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.