Pass QMap type to QML
-
Hi All,
Is it possible to pass QMap<T1,T2> type as types in Q_PROPERTY and be possible to access them in QML ?I could see we can use QVariantMap, but i dont want to pass QString as key. I need to pass an enumerator as key .
Can someone point me in correct direction.
Thanks in advance.
-
@Vinoth-Rajendran4
using an enumerator as key is not JSON conform, no way around it.
So there is no direct way to support any types other the ones mentioned here: https://doc.qt.io/qt-5/qtqml-cppintegration-data.html
For everything else you need to create custom QML types or QObject based wrappers for example. -
@raven-worx is right. Here is an example of a custom C++ type that maybe could help you:
class QCustomMap: public QObject { Q_OBJECT public : QCustomMap(); Q_INVOKABLE int getId(int id) const { return _someMap.contains(id) ? _someMap[id] : 0; } private: const static QMap<int, int> _someMap; ///< Map for your elements }
But It depends on what you want to do and your base code, maybe what you want is to use only a Q_INVOKABLE in your current QObject instead a Q_PROPERTY to get/update the values and also create/register a Q_ENUMS for you to be able to use the ENUM on QML, let me know what is your plan or architecture and I will send you some additional code as reference. I hope this would help you for the moment.
-
@Vinoth-Rajendran4 QML works well with QVariantMap