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

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

 
Vivainio shows on his blog how Python has a handy pattern to avoid crashes if a module is not available and how a work around can be created for QML to avoid this same situation.

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
}

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

property variant positionSource
Component.onCompleted: {
var s = "import QtMobility.location 1.1; PositionSource { id: positionSource; updateInterval: 1000; active: true }"
var co = Qt.createComponent(s)
if (co.status == Component.Ready) {
var o = co.createObject(statusDialogItem)
positionSource = o
} else {
console.log("QtMobility.location 1.1 not available, using placeholder")
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.

Please head on over to the original post to discuss.
Source [Confusing Developers]

About ReadWrite’s Editorial Process

The ReadWrite Editorial policy involves closely monitoring the tech industry for major developments, new product launches, AI breakthroughs, video game releases and other newsworthy events. Editors assign relevant stories to staff writers or freelance contributors with expertise in each particular topic area. Before publication, articles go through a rigorous round of editing for accuracy, clarity, and to ensure adherence to ReadWrite's style guidelines.

Get the biggest tech headlines of the day delivered to your inbox

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.

    Tech News

    Explore the latest in tech with our Tech News. We cut through the noise for concise, relevant updates, keeping you informed about the rapidly evolving tech landscape with curated content that separates signal from noise.

    In-Depth Tech Stories

    Explore tech impact in In-Depth Stories. Narrative data journalism offers comprehensive analyses, revealing stories behind data. Understand industry trends for a deeper perspective on tech's intricate relationships with society.

    Expert Reviews

    Empower decisions with Expert Reviews, merging industry expertise and insightful analysis. Delve into tech intricacies, get the best deals, and stay ahead with our trustworthy guide to navigating the ever-changing tech market.