Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
d3v3l0p3r
Posts: 168
Registered: ‎07-19-2012
My Device: Q5, Q10, Z10, Z30, PlayBook

C++ object exposed to QML with function returning complex type

I have a c++ class exposed to QML using 

qml->setContextProperty("MyObject", MyClass);

All functions returning basic types such as "int", "bool", "string"

Q_INVOKABLE int myFunction(int myID) {
return myID;
}

 have no problem returning those values back to QML when called from QML:

var myNumber = MyObject.myFunction(44);
// myNumber now is set to 44

 However when I have a function (in the same C++ class as above) which returns something like QDeclarativePropertyMap

Q_INVOKABLE QDeclarativePropertyMap* myMapFunction() {
QDeclarativePropertyMap* myMap = new QDeclarativePropertyMap;
myMap->insert("ID", QVariant(55));
return myMap;
}

 when I call it I am getting [undefined] in QML

 

var myMap = MyObject.myMapFunction();
// myMap now is [undefined]
any idea why?
---
If you find my post helpful please "like" it and "accept as a solution"
Developer
Developer
lew
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha

Re: C++ object exposed to QML with function returning complex type

i think you need to return a QVariant...if you cannot wrap the QDeclarativePropertyMap within a QVariant i.e QVariant(QDeclarativePropertyMap*), then you would need to:

1) create a custom wrapper class (with QObject defined within that class for use in QML)

2) register that custom wrapper class so that it is useable in QML: qmlRegisterType

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: C++ object exposed to QML with function returning complex type

the documentation shows only the usage by context property:
http://doc.qt.digia.com/qt/qdeclarativepropertymap.html

according to my experience with the similar list declaration you should be able to use it with Q_PROPERTY.
I found an example here:
http://www.qt.gitorious.org/qt-creator/qt-creator/blobs/a4e9f282dc67df4cba11d05c0d0b8d4582f7ad3e/src...

i did not try to use it as a q_invokable yet.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
dridk
Posts: 91
Registered: ‎09-25-2012
My Device: bb10 alpha

Re: C++ object exposed to QML with function returning complex type

[ Edited ]

use QVariantMap : 

 

Q_INVOKABLE QVariantMap myFunction(QString ID){}

 

Also, you can use Q_PROPERTY to get dynamic property : 

 

class MyClass : public QObject{
Q_PROPERTY (QVariantMap data READ data NOTIFY dataChanged)
Q_OBJECT

public:
QVariantMap data(){return mData;}
void insertData(const QString& key)
       {mData.insert(key, 42); emit dataChanged()}
signals:
void dataChanged();

private:
QVariantMap mData;

}

//From QML 
//Use the property.. will automatically changed when dataChanged has emitted
Label {
text: myobject.data["key"]

}

 

a lover of Qt