Important: Please read the Qt Code of Conduct -

What is wrong with QCompass?

  • I need device azimuth for my Android application.

    In Qt docs there is very few info about QSensors programming. Finding some examples from here and there I made some code. First I initialize compass:

    QCompass* compass;
        compass = new QCompass(this);
        compass->setDataRate(5); // Hz
        connect( compass, SIGNAL(readingChanged()), this, SLOT(gotCompassData()) );
        qWarning()<<"********************** connecting to compass"<<compass->connectToBackend();

    have got:

    : (null):0 ((null)): ********************** connecting to compass true

    Before I need azimuth call:


    And catch signal to slot:

    void MyClass::gotCompassData()
        QCompassReading* reading = compass->reading();
        if( reading != Q_NULLPTR )
            qWarning()<<"device azimuth"<<reading->azimuth();
            qWarning()<<"nothing read";

    Data are requested periodically but in console I see only this:

    (null):0 ((null)): device azimuth 0
    (null):0 ((null)): device azimuth 0
    (null):0 ((null)): device azimuth 0
    (null):0 ((null)): device azimuth 0
    (null):0 ((null)): device azimuth 0
    (null):0 ((null)): device azimuth 0
    (null):0 ((null)): device azimuth 0
    (null):0 ((null)): device azimuth 0

    in any device position.

    I am pretty sure other compass simulating applications work fine on this device.
    Result is the same with any

    compass->setDataRate(5); // 100, 500, 0... doesn't matter

    What I did wrong or what I did not?

  • Lifetime Qt Champion

    @gourmet said in What is wrong with QCompass?:


    Why do you stop the sensor in your slot?

  • @jsulm to not waste processor time when it is not neeed. It starts periodically from other thread.This does not affect the result - sensor sends "ready" data but they are zero. Why?

  • Wow! Things are more strange than I can imagine. Today I started work with this app - and suddenly have got proper azimuth values. Once. Next time when I run app - value become 0 again.

  • If I do not stop compass - it works. But it wastes resources cause it is not needed all the time. Once I stop compass when it is not needed - next app run it doesn't work. Looks like a bug.

  • Looks like I find workaround this. First I start compass in constructor of class using it. Then I stop compass little later. Next time I start it again when need azimuth and stop when get it. This way works - I get azimuth values in all program runs. But this is definitely a bug in QCompass.

Log in to reply