[SailfishDevel] Accessing C++ properties from QML

Antonio Mancini ziobilly94 at gmail.com
Thu Aug 13 17:26:50 UTC 2015


>
> for properties system you should use Q_PROPERTY macro inside your class.


Since i have to access properties of a class declared inside another class
i find using the macro Q_PROPERTY a little confusing.


> Actually, I think you're creating a new instance of the FirstPage.qml
> component using this code.  But, Sailfish should have already created its
> own instance of that page when it started up.
>
> You might instead try retrieving a view of the Sailfish instance and going
> from there, something like this:
>
> QQuickView *view = SailfishApp::createView();
> QObject *object = view->rootObject();
> QObject *label = object->findChild<QObject *>("label");
>
> --John
>

I tried this, it compiles, after including <sailfishapp.h> and
<QQuickItem>, but now there's no output, as it can't find the proper object
or the SailfishApp::createView() creates a new, empty view

The code is this:

>     ...

    QQuickView *view = SailfishApp::createView();
>     QObject *object = view->rootObject();
>     QObject *label = object->findChild<QObject*>("label");
>     QQmlProperty property(label, "text");
>
>     cout << "Read before: " << property.read().toString() << endl;
>
>     if (label) {
>         cout << "true";
>         property.write(active.name);
>     } else
>         cout << "false" << endl;
>
>     cout << "Read after: "  << property.read().toString() << endl;
>
>     fetchMonsterDone();
>
    ...
>

the output is:

> Read before:
>
> false
>
> Read after:
>
> [D] onFetchMonsterDone:51 - Fetch done
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.sailfishos.org/pipermail/devel/attachments/20150813/d41d30ff/attachment.html>


More information about the Devel mailing list