QML: How to not crash if QtMobility.location is not available

Python has a handy pattern to avoid crashes if, say, a module is not available:

try:
    import foobar
except ImportError:
    foobar_not_available = True

QML has no such feature, at least yet. That’s why the whole file “fails” at once when a module is missing, leaving you with a black screen and no error message in sight (unless you are launching the application from console). This can be a drag, especially when you want to publish both on platforms that have the feature (Symbian, Maemo, Harmattan, Qt Simulator) as well as platforms that don’t have it yet (Ubuntu desktop, MeeGo 1.1, …).

However, QML *can* instantiate objects dynamically, so instead of having this directly in your .qml file:

PositionSource {
    id: positionSource
    updateInterval: 1000
    active: true
}

move the potentially offending code above segregated to another file, “MaybePositionSource.qml”:

import QtMobility.location 1.1

PositionSource {
    id: positionSource
    updateInterval: 1000
    active: true
}

Now, you can create positionSource dynamically, and replace it with a dummy placeholder object if it’s not available:


    Component.onCompleted: {
        var co = Qt.createComponent("MaybePositionSource.qml")
        if (co.status == Component.Ready) {
            var o = co.createObject(statusDialogItem)
            positionSource = o            
        } else {
            console.log("QtMobility.location 1.1 not available, using placeholder. Error= " + co.errorString())
            positionSource = { position : { coordinate : {} } }
         }
    }

Now, reference to, say, positionSource.position.latitude evaluated to ‘undefined’ instead of causing an exception. You could use your own placeholder values if it seems more appropriate.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

6 Responses to QML: How to not crash if QtMobility.location is not available

  1. Very nice! So this most likely works the same way for the QtMobility.sensors elements like Compass, Gyroscope, etc. elements?

    • vivainio says:

      Yeah, the approach should be directly transferrable (with different kinds of dummy placeholders, of course).

      Before adding this to Twimgo, I do suggest you do svn up ;-). I added this snippet to twimgo to make meego 1.1 build work.

  2. Pingback: QML: How to not crash if QtMobility.location is not available

  3. I couldn’t get this to work with Qt Simulator, N8 or N900. It always logs “QtMobility.location 1.1 not available, using placeholder” even though PositionSource works if it is fixed to QML.

    Does it work for you in Qt Simulator? Did you try running it on N8/E7?

    • vivainio says:

      It appears I had got false positive from the non-placeholder code working in my N900 (I accidentally ran a stale version). I have now fixed the blog post so that the code actually works :). It’s a bit cleaner as well.

      Please try it out (and fix the code in twimgo ;-).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s