Hi All

Just a quick note to say that

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

works (so far) without error if Lucien's patch to sailfishapplication.cpp is applied. This can be found here: https://lists.sailfishos.org/pipermail/devel/attachments/20130802/2367c686/attachment.cpp

Chris

----- Weitergeleitete Nachricht von christopher.l...@thurweb.ch -----
     Datum: Sun, 04 Aug 2013 18:36:06 +0200
       Von: christopher.l...@thurweb.ch
Betreff: Re: [SailfishDevel] Alpha 2: passing simple properties from main.cpp to qml: A bug?
        An: "Lucien XU" <sfietkonstan...@free.fr>
        Cc: devel@lists.sailfishos.org

Salut Lucien

Maybe it is clearer with Code examples

I was doing this, which gives me an error, but seems to work!

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

You suggest I could this:

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

..but will then run into your template error:
file:///usr/share/setContextPropertyDemo/main.qml: File not found

Maybe the qmlRegister**Type is the best approach after all. The downside is (unless I have missed something) that I will then have to create a class for my property, whereas with the setContextProperty I can use a string literal or a primitive type like int.

Merci

Chris


Zitat von "Lucien XU" <sfietkonstan...@free.fr>:

Hello guys !

It might be useful to use qmlRegister**Type (uncreatable, singleton etc.) to
expose C++ components to QML. But if you really want to use rootContext()-
setContextProperty, you have to be cautious.

Actually, if you pass the qml file to the view before registering C++
components, the QML components won't be able to see the C++ ones, so you have
to call setContextProperty before calling setSource (or calling the
createView("main.qml") method).

You have to call the parameter-less method createView(), and then, call
setView(QQuickView *, QString) to pass the source QML file.

But beware, there is a bug in the template. See
https://lists.sailfishos.org/pipermail/devel/2013-August/000529.html.

Hopes it helps (because I'm not very clear in this mail)
Cheers,
Lucien

Le dimanche 4 août 2013 17:05:01 christopher.l...@thurweb.ch a écrit :
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
text.

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

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!

Chris

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[])
{
    QScopedPointer<QGuiApplication>
app(Sailfish::createApplication(argc, argv));
    QScopedPointer<QQuickView> view(Sailfish::createView("main.qml"));
    view->rootContext()->setContextProperty("cppproperty",  "Hello world");
Sailfish::showView(view.data());

    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.gri...@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.l...@thurweb.ch <christopher.l...@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

_______________________________________________
SailfishOS.org Devel mailing list
_______________________________________________
SailfishOS.org Devel mailing list



_______________________________________________
SailfishOS.org Devel mailing list


----- Ende der weitergeleiteten Nachricht -----

_______________________________________________
SailfishOS.org Devel mailing list

Reply via email to