How to effectively expose many C ++ data elements for a single object in QML?

I have a pointer to a C ++ object with many data elements. I need all of them in QML. One obvious way would be to create Q_PROPERTY for each element so that they can be individually accessed through QML. However, if we are talking about dozens of data members, it’s good that there are many Q_PROPERTY lines, not to mention the need to separately treat them in QML as separate properties (especially when it comes to β€œswap” signals for each property).

I am wondering if it is possible to create one Q_PROPERTY that will include all the data elements I need. But it is not clear to me if there is a clear mismatch between the types supported by QML and the types that you can list in Q_PROPERTY . For example, in QML we have a base string , but its corresponding entry in C ++ Q_PROPERTY should be QString :

  Q_PROPERTY(QString datastring READ showdata NOTIFY datastringChanged) //in QML, `datastring` is just a plain string 

Were there more complex properties like lists or arrays that could easily be matched? QML is of type list , while C ++ has QList , but are they the same? Where can I find a list of compatible types between C ++ and QML?

On the other hand, having a separate Q_PROPERTY for each data member can most likely be more productive (my data is large and often changes), since QML may not need to be analyzed.

+6
source share
1 answer

Would there be more complex properties, such as lists or arrays, that might easily fit? QML is a list type, while C ++ is a QList, but is it the same thing? Where can I find a list of compatible types between C ++ and QML?

See the C ++ / JS data conversion help page . I think the list missed that QList<QObject*> also possible.

On the other hand, having a separate Q_PROPERTY for each data item is likely to be better (my data is large and often change), since QML does not need to be parsed, perhaps.

Maybe yes, it depends on your performance needs. C ++ QList converts to JavaScript list when accessed from QML / JS. This is actually a bit of an overhead. In addition, if the item in the list changes, you need to give a notification signal for the full property, and you need to review all the JS bindings in which the list was used, which again will be a large number of list transitions. It may be better due to the presence of finer-grained properties, but it really depends.

Btw, with Qt 5.1 there is now MEMBER , which makes writing Q_PROPERTY easier.

+4
source

All Articles