Cannot set QVariantMap as QML property

5.8k Views Asked by At

I want to export a QVariantMap property to QML, so I'm doing

Q_PROPERTY(QVariantMap myData READ myData)

and in myData() function I just do

QVariantMap map;
map.insert("ExampleKey", "key");
return map;

and in QML either

myData.ExampleKey //undefined

or

myData["ExampleKey"] //undefined

Could someone point me in the right direction to be able to use this as a simple JS object?

1

There are 1 best solutions below

1
On

Thanks BaCaRoZzo for finding the optimal answer. Basically QML JavaScript uses QVariantMap for its array. Nothing more needs to be done, use JavaScript array and you get QVariantMap and it is transportable to C++.

The QML engine provides automatic type conversion between QVariantList and JavaScript arrays, and between QVariantMap and JavaScript objects.

http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qvariantlist-and-qvariantmap-to-javascript-array-and-object