K
K
Kalombyr2018-04-03 18:48:59
Qt
Kalombyr, 2018-04-03 18:48:59

QMap in QML what is the best way to do it?

Good day!
I am translating a project from widgets to QML.
Properties (Q_PROPERTY) appeared, in which the type should be QMap, I found that you can only either QVariantMap or QList, which is not suitable, or write wrappers for each property, which in my opinion is somehow a crutch.
Of course, even if you register the type through qRegisterMetaType, then I can’t work in QML like with a regular array =(
You can dodge somehow so as not to create a bunch of wrappers for transforming QMap into QVariantMap and back, but what would QML be possible to do? work like with a regular array "m[4]=42;" ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ighor July, 2018-04-03
@IGHOR

This should help https://stackoverflow.com/questions/24362946/how-c...
Alternative doc.qt.io/qt-5/qtcore-tools-customtype-example.html

P
Pavel K, 2018-04-03
@PavelK

In the general case, as already advised, create your own type, but still it will not work with it as with an array m[4]=42; would be something like m.set(4,42); and besides, again, you will most likely have to write "wrappers", or overload the [] operator, and so on - depending on your situation.
You can work like with a regular array through QList, but there will be QList features - if you do m[4]=42 in QML; then from 0 to 3 the list will be filled with zeros and you may have to make more changes to the code to bypass this feature.
In general, theoretically, you can inherit from QVariant by overloading the = operator to perform internal conversion of QMap to QVariantMap , put it all neatly into QVariant and overload () to perform the reverse conversion. It will be a little difficult, but it is possible. Or perhaps make the Qt meta-system think of QMap as a QVariant map.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question