[SailfishDevel] Accessing C++ properties from QML
Andrea Bernabei
and.bernabei at gmail.com
Thu Aug 13 13:17:33 UTC 2015
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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.sailfishos.org/pipermail/devel/attachments/20150813/9bb8956b/attachment.html>
More information about the Devel
mailing list