[SailfishDevel] Accessing C++ properties from QML

Andrey Kozhevnikov coderusinbox at gmail.com
Thu Aug 13 15:25:23 UTC 2015


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

------ Исходное сообщение ------
От: "Antonio Mancini" <ziobilly94 at gmail.com>
Кому: "Sailfish OS Developers" <devel at lists.sailfishos.org>
Отправлено: 13.08.2015 18:14:29
Тема: [SailfishDevel] Accessing C++ properties from QML

>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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.sailfishos.org/pipermail/devel/attachments/20150813/d854f8ec/attachment.html>


More information about the Devel mailing list