Sailfishapplication and setContextProperty

I’m very excited on the last 2 days since the Sailfish OS Alpha SDK was released. I’m working on a new project for harmattan and hence I would like to test it out how easy it was to port the UI from Harmattan to Sailfish Silica. Well, that’s another story hence I won’t cover it in this post. All I want to share with you is that I faced a little problem when I want to setContextProperty() to share a list model from Qt to QML.

 

 

For example, if I want to share a QAbstractListModel called “piModel” to QML, here’s the code


view->rootContext()->setContextProperty("listModel", piModel);

view->setSource(QUrl("qrc:///main.qml");

Then in QML ListView, just set the model: listModel and the list will appear and automatically updated should there be any addition/delete in the Qt side. One important note is setContextProperty() will only works if you call it before setSource(). But that’s the problem with the default sailfishapplication because the QDeclarativeView initialization and setSource() will be inside a function called Sailfish::CreateView(QString&). Therefore, if you setContextProperty() after the Sailfish::CreateView() line, it won’t work!

Inspired by this blog post: http://meegoharmattandev.blogspot.sg/2011/11/qmlapplicationviewer-and.html on the workaround in Harmattan, I’ve created a similar hack to the sailfishapplication.h and sailfishapplication.cpp file inside the sailfishapplication directory. Basically the workaround is seperate the Sailfish::CreateView() function to 2 parts, the first part just initialize and return the QDelcarativeView pointer and the second part responsible for setting the source file. Hence, you can call the setContextProperty() function in between the first and second part.

The code is a little bit lengthy and messy if I embed in this post, therefore you can take a look at the modified sailfishapplication code section and also the sample main.cpp file at my Github Gisthttps://gist.github.com/cckwes/5053643 . Do leave a comment here if you found any bug or have any better solution. Happy coding :)

P/S: The code might be overwrite you choose to update the sailfishapplication codes automatically when the SDK version is updated.

2 thoughts on “Sailfishapplication and setContextProperty

  1. Whoa, thx a bunch for this tip! Was wasting hour after hour trying to debug why my setContextProperty won’t work!

    Did you report this to the Jolla ppl? They need to fix this…

    • Not yet, no idea where to report the bug. I guess this issue exists since Harmattan where you need to tweak the qmlapplicationviewer.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

Follow me on Google+

Meta

Switch to our mobile site