Qt 5.3 Can't make QCompass (QSensor) work on W

2019-07-04 06:18发布

问题:

I can't make sensors works on my Asus Transformer T100.

Magnetometer and Compass don't start, and I have fake value from the accelerometer (always x=0, y=9.8, z=0).

I always get the same result, even with my laptop :

first textEdit:

Initialisation...
QAccelerometer is connected to backend...
QAccelerometer isActive...
Attente des données capteur...

Second textEdit:

ven. juin 6 14:56:41 2014
Acceleration:  x = 0
y = 9.8
z = 0

Compass: UNAVAILABLE
QMagnetometer: UNAVAILABLE

And this code :

QList<QByteArray> sensorList = QSensor::sensorTypes();
ui->init->append("Sensor list length: " + QString::number(sensorList.size()).toUtf8());
foreach( QByteArray sensorName, sensorList ) {
    ui->init->append("Sensor: " + sensorName);
}

Give me :

Sensor: QAmbientLightSensor
Sensor: QAccelerometer
Sensor: QTiltSensor
Sensor: QOrientationSensor
Sensor: QRotationSensor

Where is QCompass ? QMagnetometer ? Why QAccelerometer is faked ? :'(

Here is my simplified test-code, only with QCompass :

header:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QCompass>
#include <QCompassReading>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void update();
    void error(int);

private:
    Ui::MainWindow *ui;

    QCompass *compass;
    QCompassReading *compass_reading;
};

#endif // MAINWINDOW_H

code :

#include <QDateTime>
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->init->setText("Initialisation...");

    compass = new QCompass(this);
    connect(compass, SIGNAL(readingChanged()), this, SLOT(update()));
    connect(compass, SIGNAL(sensorError(int)), this, SLOT(error(int)));
    compass->setDataRate(1);
    compass->start();
    if (compass->isBusy()) {
        ui->init->append("QCompass is busy...");
    }
    if(compass->isConnectedToBackend()) {
        ui->init->append("QCompass is connected to backend...");
    }
    if(compass->isActive()) {
        ui->init->append("QCompass isActive...");
    }

    ui->init->append("Waiting for sensors...");
}

MainWindow::~MainWindow()
{
    delete compass;
    delete ui;
}

void MainWindow::update()
{
    QString text_compass;

    ui->textEdit->clear();

    accel_reading = accel->reading();

    compass_reading = compass->reading();
    if(compass_reading != 0) {
        text_compass = QDateTime::currentDateTime().toString() +
                + "\nCompass:  azimuth = " + QString::number(compass_reading->azimuth());
                + "\ncalibration level = " + QString::number(compass_reading->calibrationLevel());

        ui->textEdit->append(text_compass);
    }
    else {
        text_compass = "\nCompass: UNAVAILABLE";
        ui->textEdit->append(text_compass);
    }
}


void MainWindow::error(int erreur) {
    QMessageBox::critical(this, "Erreur", "Erreur num : " + QString::number(erreur).toUtf8());
}

回答1:

The solution for now is to hack the winrt sensors plugin and rebuild it. (the sensors backend for winrt works for windows 7/8 desktop application).

First git clone the plugin, or get Qt's source code.

then open src/plugins/sensors/sensors.pro and add these lines :

win32-msvc2012|win32-msvc2013 {
    isEmpty(SENSORS_PLUGINS): SENSORS_PLUGINS = winrt generic dummy
}

winrt|win32-msvc2012|win32-msvc2013 {
    isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, winrt):SUBDIRS += winrt
} 

REMOVE this line :

isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, winrt):winrt:SUBDIRS += winrt 

Then open src/plugins/sensors/winrt/winrt.pro and add this lines :

win32-msvc2012|win32-msvc2013: LIBS += -lruntimeobject

And finally run qmake, make/nmake, make/nmake install (Ask google for more informations about how to build)

IMPORTANT : the run process should start from the directory where the qtsensors.pro file is (e. g. C:\Qt\5.4\Src\qtsensors).

To get more informations, and follow :

  • Link to the bug : https://bugreports.qt-project.org/browse/QTBUG-39590
  • Link to the patch : https://codereview.qt-project.org/#/c/87370/


标签: c++ qt qt5