×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Spain
    Posts
    15

    Question QML + QtMobility: proper way to access sensor API

    I don't know if this post belongs here, as it's about both QML and QMobility.

    I successfully accessed rotation and accelerometer data with QML, but I'm currently struggling with AmbientLightSensor and ProximitySensor.

    There are enough examples of accessing sensor data with Qt/C++, but QML seems different.

    I do something like this:
    Code:
    Item {
        id: sensor
        signal lightChanged    (int reading)
        onLightChanged: {
            console.log(reading)
        }
    
        AmbientLightSensor {
            id:lsensor
            active: true
            onReadingChanged: {
                sensor.lightChanged (reading)
            }
        }
    }
    But I get all 0's in the console, although I change ambient light values in the simulator.
    Same with the phone: the values don't seem to change.

    The documentation doesn't help much, as the only thing it says is "The AmbientLightSensor element wraps the QAmbientLightSensor class".

    So what would be the proper way to access AmbienLightSensor from QML?

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    Re: QML + QtMobility: proper way to access sensor API

    I'm sorry, I don't know the answer to your question, but I would like to know how you accesed the Accelerometer and orientation sensor as I can't seem to find anything on the web

  3. #3
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QML + QtMobility: proper way to access sensor API

    Quote Originally Posted by Seikeau View Post
    I can't seem to find anything on the web
    http://doc.qt.nokia.com/latest/qmlvi...ing-the-object

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Spain
    Posts
    15

    Re: QML + QtMobility: proper way to access sensor API

    Quote Originally Posted by Seikeau View Post
    I'm sorry, I don't know the answer to your question, but I would like to know how you accesed the Accelerometer and orientation sensor as I can't seem to find anything on the web
    I used this to get rotation data (in angles, but not for orientation in the form of portrait, landscape, etc)
    Code:
    Item {
        id: sensor
        signal rotationChanged (real xr, real yr, real zr)
        property real rx
        property real ry
        property real rz
    
        RotationSensor {
            id: rsensor
            active: true
            onReadingChanged: {
                sensor.rotationChanged(reading.x, reading.y, reading.z)
            }
        }
        onRotationChanged: {
            rx = xr
            ry = yr
            rz = zr
        }
    }
    The accelerometer is just the same changing RotationSensor to Accelerometer.
    For things like knowing if the device is in portrait, landscape, etc. in ended up implementing the "runtime" object in C++ (using QOrientationSensor), as in Qt 4.7.2 seems to not be available (only available in the qmlviewer app).

    I used this as a template: http://cdumez.blogspot.com/2010/12/s...n-for-qml.html

    I don't know why it wasn't made available to QML, because is not that difficult.

  5. #5
    Regular Contributor
    Join Date
    Jul 2007
    Location
    Eindhoven, the Netherlands
    Posts
    187

    Re: QML + QtMobility: proper way to access sensor API

    Hi,

    I don't think the 'signal' in the wrapper class is needed, a simple function should work fine:

    Code:
    Item {
        id: sensor
        property real rx
        property real ry
        property real rz
    
        function rotationChanged (real x, real y, real z) {
            rx = x
            ry = y
            rz = z
        }
    
        RotationSensor {
            id: rsensor
            active: true
            onReadingChanged: {
                sensor.rotationChanged(reading.x, reading.y, reading.z)
            }
        }
    }
    There seems to be very little documentation available on the qml sensor api, but i would expect the following to work fine as well:
    Code:
    Item {
        id: sensor
        property real rx: rsensor.x
        property real ry: rsensor.y
        property real rz: rsensor.z
    
        RotationSensor {
            id: rsensor
            active: true
        }
    }
    As for the ambient light sensor, the following is currently in the QtMobility 1.2 master tree header file for the ambient sensor:
    Code:
        Q_PROPERTY(LightLevel lightLevel READ lightLevel)
        DECLARE_READING(QAmbientLightReading)
    public:
        enum LightLevel {
            Undefined = 0,
            Dark,
            Twilight,
            Light,
            Bright,
            Sunny
        };
    If i read this correctly, then the following should work:
    Code:
    Item {
        id: sensor
        property int level: rsensor.lightLevel
    
        AmbientLightSensor {
            id: rsensor
            active: true
        }
    }
    Note that i haven't tested any of this... (yet ;-)


    Regards,
    Mark.
    Last edited by hurenkam; 2011-03-21 at 16:03.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Spain
    Posts
    15

    Smile Re: QML + QtMobility: proper way to access sensor API [SOLVED]

    Thanks for your help!

    You're right on the unneeded signal wrapper. Your first code snippet works great, except for the variable types in the rotationChanged function, which aren't needed (they actually throw an error if used).
    So basically it should be changed to function rotationChanged(x,y,z)

    The second snippet doesn't work because rsensor.x et al aren't defined.


    For getting AmbientLightSensor to work I had to use the first approach too. Again thanks for pointing to the "lightLevel" property.
    It works like this:
    Code:
    Item {
        id: sensor
        property int light
    
        function lightChanged (l)
        {
            light = l.lightLevel
        }
        AmbientLightSensor {
            id:lsensor
            active: true
            onReadingChanged: {
                sensor.lightChanged (reading)
            }
        }
    }
    The ProximitySensor works just the same, but the property to read is close of type bool, instead of lightLevel.

  7. #7
    Regular Contributor
    Join Date
    Jul 2007
    Location
    Eindhoven, the Netherlands
    Posts
    187

    Re: QML + QtMobility: proper way to access sensor API [SOLVED]

    Hi,

    Glad to hear you got things to work.
    In hindsight, i guess it makes sense that the properties can not be read directly from the sensor itself, but need to be read from the reading, since that is also how the c++ classes work.

    Perhaps the following works also:
    Code:
    Item {
        id: sensor
        property int light: lsensor.reading.lightLevel
    
        AmbientLightSensor {
            id:lsensor
            active: true
        }
    }
    But i'm not entirely sure that binding to an attribute of reading will work properly with respect to automatic updates.

    Mark.
    Last edited by hurenkam; 2011-03-21 at 23:46.

  8. #8
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    56

    Re: QML + QtMobility: proper way to access sensor API

    Thanks for rotation sensor code, just now any idea how to make it run in simulator? (I know there's no accelerometer on PC, but it could at least just ignore it), and also designer doesn't work until I remove/comment out code related to sensors, it says "module "QtMobility.sensors" is not installed (3:1)", but it works on Nokia N8. So any way I could work with designer and/or simulator without commenting out sensor related lines everytime?

  9. #9
    Regular Contributor
    Join Date
    Jul 2007
    Location
    Eindhoven, the Netherlands
    Posts
    187

    Re: QML + QtMobility: proper way to access sensor API

    Hi

    If you are unable to use the QtMobility API in your simulator, then i think something is wrong in your setup. I suggest you install the SDK from scratch, preferably use the Nokia Qt 1.1 Beta SDK, it will include the QtMobility 1.1.1 API for symbian, meego, and simulator environment.

    If you need help fixing your environment, then you'll probably get more response in the Qt SDK and Tools forum: http://discussion.forum.nokia.com/fo...SDKs-and-Tools )

    Regards,
    Mark.

  10. #10
    Regular Contributor
    Join Date
    Aug 2008
    Posts
    56

    Re: QML + QtMobility: proper way to access sensor API

    It is scratch installed without any changes, latest beta. It said about some missing dll file which obviously is where it belongs.

Similar Threads

  1. QtMobility and QML
    By angusr in forum [Archived] Qt Mobility Project
    Replies: 2
    Last Post: 2010-12-23, 03:52
  2. Replies: 0
    Last Post: 2009-11-26, 14:04
  3. Proper way to check for network connection
    By bercobeute in forum Symbian
    Replies: 0
    Last Post: 2006-12-13, 11:34
  4. Proper way to add to project?
    By smueller in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 3
    Last Post: 2005-01-13, 19:51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×