[SailfishDevel] C++ and QML Integration: SetContextProperty with the latest SailfishOS Templates

christopher.lamb at thurweb.ch christopher.lamb at thurweb.ch
Thu Dec 12 13:16:53 UTC 2013


Hi All

what is now the recommended way to SetContextProperty in the main  
function using the latest Sailfish application templates (from thp)?

The default main now looks like:

#include <sailfishapp.h>

int main(int argc, char *argv[])
{

     return SailfishApp::main(argc, argv);
}

In the days of the old template and sailfishapplication I used to do  
it this way:

#include "sailfishapplication.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
{
     QScopedPointer<QGuiApplication>  
app(Sailfish::createApplication(argc, argv));

     //New Sailfish Alpha 2 /Qt 5 way
     //requires patch to sailfishapplication.cpp from Lucien Xu
     QScopedPointer<QQuickView> view(Sailfish::createView());
     view->rootContext()->setContextProperty("cppproperty",  "Hello world");
     Sailfish::setView(view.data(), "main.qml");

     Sailfish::showView(view.data());
     return app->exec();
}

Thanks

Chris





More information about the Devel mailing list