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

Posts: 181
Registered: ‎07-19-2012
My Device: Q5, Q10, Z10, Z30, PlayBook
My Carrier: Carrier

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"
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha
My Carrier: -

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


Posts: 17,022
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

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

the documentation shows only the usage by context property:

according to my experience with the similar list declaration you should be able to use it with Q_PROPERTY.
I found an example here:

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
Posts: 91
Registered: ‎09-25-2012
My Device: bb10 alpha
My Carrier: free

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)

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

QVariantMap mData;


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



a lover of Qt