QCompass class returns 0.0 azimuth



  • Hey
    Using QCompass i am trying to find out the azimuth angle. I am using Qt SDK 4.6.3 and Qt Mobility 1.0.2

    Here is my code:

    @compass = new QCompass(this);
    compass->start();
    if(!compass->isActive())
    {
    QMessageBox mb1;
    mb1.setText("Compass Not ready");
    }
    QCompassReading *compassReading = compass->reading();
    QString output = QString("Compass: %1 azimuth, %2 cl").arg(compassReading->azimuth()).arg(compassReading->calibrationLevel());
    ui->textEdit->setText(output);
    compass->stop();@

    on my device [E72] it returns Compass: 0 Azimuth cl 0.

    What did i miss?



  • As I see it you are starting the compass and query a reading immediately. I guess there is no reading available yet. You should wait for the readingChanged signal of QSensor and then query the reading.



  • Hi, ~mannu

    You want to get information from compass before it has it. The best solution will be to connect QCompass* with your slot.

    @
    void MyClass::method()
    {
    QCompass * compass = new QCompass(this);
    connect(compass, SIGNAL(readingChanged()), this, SLOT(mySlot()));
    }

    void MyClass::MySlot()
    {
    QCompass * compass = (QCompass*)sender();
    QCompassReading* cr = compass->reading();
    QString output = QString("Compass: %1 azimuth, %2 cl").arg(cr->azimuth()).arg(cr->calibrationLevel());
    ui->textEdit->setText(output);
    compass->stop();
    }

    @

    UPD: you should use "QSignalMapper":http://qt-project.org/doc/QSignalMapper here to check sender()



  • I would strongly advice against using the code posted by tucnak. It is unsafe. If the code is triggered by something else than the signal of QCompass, it will cause a crash as the cast will return nonsense. In general, I advice against using the sender() method at all, except for some specialized cases like building [[doc:QSignalMapper]] like classes.

    Also, I wonder why the code calls compass->stop()?



  • [quote author="Andre" date="1341229348"]I would strongly advice against using the code posted by tucnak. It is unsafe. If the code is triggered by something else than the signal of QCompass, it will cause a crash as the cast will return nonsense. In general, I advice against using the sender() method at all, except for some specialized cases like building [[doc:QSignalMapper]] like classes.

    Also, I wonder why the code calls compass->stop()? [/quote]

    Yes, I agree with you. But it was fast-code (like an example). Of course we should to check sender.



  • [quote author="tucnak" date="1341230465"] Yes, I agree with you. But it was fast-code (like an example). Of course we should to check sender. [/quote]
    Then please don't post such code. People are bound to copy/paste your solution, think that it works properly and use it in production code.

    The use of sender() here is nonsense anyway. If you assume that the method will only be called by the QCompass object, then you might as well just reference a pointer to that object directly.

    [edit fixed quote, koahnig]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.