[SailfishDevel] Alpha 2: passing simple properties from main.cpp to qml: A bug?

christopher.lamb at thurweb.ch christopher.lamb at thurweb.ch
Sun Aug 4 15:05:01 UTC 2013

Hi Martin

Curiouser and Curiouser!

Just to be sure, I went back and tested your suggestion in a demo  
project based on the default "Hello Sailors" new project template.

Here I pass the c++ property cppproperty with the value "Hello World".  
In the QML FirstPage this is concatenated to the "Hello Sailors" label  

I still get this error:
file:///opt/sdk/setContextPropertyDemo/usr/share/setContextPropertyDemo/pages/FirstPage.qml:34: ReferenceError: cppproperty is not  

Yet the app displays "Hello Sailors: Hello world".

So a property can be passed as you suggested, it can be used. The  
error must be wrong!


P.s you test the same by creating a default new Sailfish project, and  
replacing the main.cpp and FirstPage.qml with the code below.

//start main.cpp
#include <QGuiApplication>
#include <QQuickView>
#include <QQmlContext>

#include "sailfishapplication.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
app(Sailfish::createApplication(argc, argv));
     QScopedPointer<QQuickView> view(Sailfish::createView("main.qml"));
     view->rootContext()->setContextProperty("cppproperty",  "Hello world");

     return app->exec();
//end main.cpp

//start FirstPage.qml
import QtQuick 2.0
import Sailfish.Silica 1.0
import Sailfish.Silica.theme 1.0

Page {
     id: page

     // To enable PullDownMenu, place our content in a SilicaFlickable
     SilicaFlickable {
         anchors.fill: parent

         // PullDownMenu and PushUpMenu must be declared in  
SilicaFlickable, SilicaListView or SilicaGridView
         PullDownMenu {
             MenuItem {
                 text: "Show Page 2"
                 onClicked: pageStack.push(Qt.resolvedUrl("SecondPage.qml"))

         // Tell SilicaFlickable the height of its content.
         contentHeight: childrenRect.height

         // Place our content in a Column.  The PageHeader is always  
placed at the top
         // of the page, followed by our content.
         Column {
             width: page.width
             spacing: Theme.paddingLarge
             PageHeader {
                 title: "UI Template"
             Label {
                 x: Theme.paddingLarge
                 text: "Hello Sailors: " + cppproperty
                 color: Theme.secondaryHighlightColor
                 font.pixelSize: Theme.fontSizeLarge
//end FirstPage.qml

Zitat von "Martin Grimme" <martin.grimme at gmail.com>:

> Hi,
> you don't want to set the context property on the
> QmlApplicationViewer, which was dead Nokia code to target Symbian and
> Harmattan, anyway.
> QmlApplicationViewer was derived from QmlView, where it got the
> rootContext() method from.
> With Qt5, QmlView was renamed to QQuickView. And when using the
> functions from sailfishapplication.h, you get a QQuickView*, e.g.:
>         QScopedPointer<QQuickView> view(Sailfish::createView("main.qml"));
>         view->rootContext()->setContextProperty("platform", platformId);
> Martin
> 2013/8/4, christopher.lamb at thurweb.ch <christopher.lamb at thurweb.ch>:
>> Hi all
>> What is the recommended way to pass simple c++ properties from the
>> main.cpp to the QML part of a project using the Qt 5 Alpha?
>> In the good old days of the first Alpha, I used to do this Harmattan
>> style using
>>      QmlApplicationViewer viewer;
>>      viewer.rootContext()->setContextProperty("platform",  platformId);
>> This would make the c++ property platformId available to my qml code
>> as the property platform.
>> Now with Qt5 / Alpha 2 the QmlApplicationViewer is no longer created.
>> The tutorial linked below suggests that the QmlApplicationViewer files
>> could be ported to Qt5, so I guess I could grab these files from an
>> old Harmattan project and do that.
>> https://qt-project.org/doc/qt-5.0/qtdoc/portingqmlapp.html
>> But somehow that feels like carting old baggage around.
>> Is their a Sailfish native way of doing the same?
>> Thanks
>> Chris
>> _______________________________________________
>> SailfishOS.org Devel mailing list

More information about the Devel mailing list