[SailfishDevel] Accessing C++ properties from QML
Petr Vytovtov
osanwe at protonmail.ch
Fri Aug 14 04:25:46 UTC 2015
You can try to use signals and slots like this:
Header
class // ...
{
// ...
signals:
void somethingWasDone();
// ....
}
Source
// ...
someMethod() {
// Do something ....
emit somethingWasDone();
}
// ...
QML
// ...
Connections {
target: // Your module
onSomethingWasDone: // Change properties
}
// ...
More about this you can see at the video: https://www.youtube.com/watch?v=R59lpwNy9WE
Regards,
Petr
[Linux User Group Udmurtia](https://udmlug.wordpress.com/)
-------- Original Message --------
Subject: Re: [SailfishDevel] Accessing C++ properties from QML
Time (UTC): August 13 2015 5:26 pm
From: ziobilly94 at gmail.com
To: devel at lists.sailfishos.org
CC:
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/20150814/215f1676/attachment.html>
More information about the Devel
mailing list