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]