Expose QHash to QML
-
I would like to expose a dictionary (QHash<Qstring, int>) to QML UI. If I try it the easy way using Q_PROPERTY, I get following error: QMetaProperty::read: Unable to handle unregistered datatype 'QHash<QString,int>' for property 'Config::options'
Is there any easy solution to handle this or do I have to implement a QAbstractListModel to expose the data to QML? It seems like an overkill to me.
-
Hi,
When using custom types like that, you need to register them with the Qt metatype system.
-
I'm pretty new to Qml so I'm not entirely sure, but I think you can only make properties of C++/Qt types that have a matching type in Qml. I'm not sure what the best approach would be in your case. Maybe just registering the type in addition to what you're doing would work. Idk. Please report your solution once you find it as I'm curious myself.
-
I found 2 good solutions mentioned on Stack Overflow.
First I tried to implement the solution using QVariantMap. But I had problems with changing the values back from QML. So I tried the second option and used QQmlPropertyMap. This is a very easy solution which doesn't even need to implement any signals when data are changed or writing functions to get changed data from QML.
This is the solution I was looking for.