[SailfishDevel] Accessing C++ properties from QML

Antonio Mancini ziobilly94 at gmail.com
Thu Aug 13 14:51:45 UTC 2015


Thanks, actually i had already seen that but couldn't accomplish much, now
i tried again and i managed to access and edit qml properties, but the QML
object doesn't update.

i use this code in a c++ method

    QQmlEngine engine;
>     QQmlComponent component (&engine,
> "/usr/share/harbour-pocketmonsters/qml/pages/FirstPage.qml");
>     QObject *object = component.create();
>     QObject *label = object->findChild<QObject*>("label");
>
>     cout << label->property("text").toString();
>
>     if (label)
>         label->setProperty("text", active.name);
>
>     cout << label->property("text").toString();
>

the output of cout is correct, but the label doesn't update

The console shows these errors, but i think they are irrelevant since the
output is correct

[W] unknown:108 - file:///usr/lib/qt5/qml/Sailfish/Silica/Page.qml:108:
> ReferenceError: __silica_applicationwindow_instance is not defined
>
> [W] unknown:94 - file:///usr/lib/qt5/qml/Sailfish/Silica/Page.qml:94:
> ReferenceError: __silica_applicationwindow_instance is not defined
>
> [W] unknown:178 - file:///usr/lib/qt5/qml/Sailfish/Silica/Page.qml:178:
> ReferenceError: __silica_applicationwindow_instance is not defined
>
> [W] unknown:331 -
> file:///usr/lib/qt5/qml/Sailfish/Silica/private/PulleyMenuBase.qml:331:
> ReferenceError: __silica_applicationwindow_instance is not defined
>

2015-08-13 15:17 GMT+02:00 Andrea Bernabei <and.bernabei at gmail.com>:

> Hi,
>
> try having a look at this
> http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html
>
> 2015-08-13 14:14 GMT+01:00 Antonio Mancini <ziobilly94 at gmail.com>:
>
>> Hi all, i need to access a property of a C++ Class, declared inside
>> another class, from QML.
>>
>> This is the principal class:
>>
>> #ifndef MANAGER_H
>>> #define MANAGER_H
>>>
>>> #include <QObject>
>>> #include <QTextStream>
>>> #include <QFile>
>>> #include "monster.h"
>>> #include "move.h"
>>> #include "turnmanager.h"
>>>
>>>
>>> class Manager : public QObject
>>> {
>>>     Q_OBJECT
>>>
>>> public:
>>>     explicit Manager(QObject *parent = 0);
>>>
>>>     int activeId;
>>>     QString activeString;
>>>
>>>     int enemyId;
>>>
>>>     Monster active;
>>>     Monster enemy;
>>>
>>>     Move activeMove;
>>>     Move enemyMove;
>>>
>>>     Q_INVOKABLE void saveActive();
>>>     Q_INVOKABLE void fetchMonster( const int &level );
>>>     Q_INVOKABLE void assignMoves(Monster &monster );
>>>     Q_INVOKABLE void setMove( Move &move, const int &id );
>>>
>>> signals:
>>>
>>>     void fetchMonsterDone();
>>>
>>> public slots:
>>>
>>> };
>>>
>>> #endif // MANAGER_H
>>>
>>
>> this is the Monster Class:
>>
>> #ifndef MONSTER_H
>>> #define MONSTER_H
>>>
>>> #include <QObject>
>>> #include <QString>
>>> #include <math.h>
>>> #include "move.h"
>>>
>>>
>>> class Monster : public QObject
>>> {
>>>     Q_OBJECT
>>>
>>> public:
>>>
>>>     explicit Monster(QObject *parent = 0);
>>>
>>>     ...
>>>
>>>     QString name;
>>>
>>>     ...
>>> };
>>>
>>> #endif // MONSTER_H
>>>
>>
>> in my qml page i include the class properly and i have something like
>> this:
>>
>> Page {
>>>
>>     id: page
>>>
>>     Manager {
>>>
>>         id: manager
>>>
>>         onFetchMonsterDone: {
>>>
>>              console.log("Fetch done")
>>>         }
>>>
>>     }
>>>
>>     Label {
>>>
>>         id: label
>>>
>>     }
>>>
>> }
>>>
>>
>> after doing some operations with fetchMonster(), the "active" class has
>> been assigned some values to its properties, i need to access these values
>> from QML in the "onFetchMonsterDone", let's say i need to assign to
>> label.text the value of manager.active.name
>>
>> OR
>>
>> i read there are some ways to access end edit QML properties from C++
>>
>> Can someone help me?
>>
>> _______________________________________________
>> SailfishOS.org Devel mailing list
>> To unsubscribe, please send a mail to
>> devel-unsubscribe at lists.sailfishos.org
>>
>
>
> _______________________________________________
> SailfishOS.org Devel mailing list
> To unsubscribe, please send a mail to
> devel-unsubscribe at lists.sailfishos.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.sailfishos.org/pipermail/devel/attachments/20150813/af4af85f/attachment-0001.html>


More information about the Devel mailing list