Custom type with typedef's in QML
-
Hello all! I have the issue connected with the custom types in QML. I have tried a lot of different approaches, so my actual code is little messy now to be posted here as "what I have done" thing. What is the correct way to use/register/declare (qRegisterMetaType, Q_DECLARE_METATYPE) custom type in QML in the following situation. I have some custom typedefs like:
typedef quint8 ADCValue; typedef quint32 ValuePosition;Then I have structure with this variables:
struct Example { Q_GADGET Q_PROPERTY(PhotodiodePosition position MEMBER m_position) Q_PROPERTY(PhotodiodeADC whiteLevel MEMBER m_whiteLevel) public: ADCValue adc = 0; ValuePosition value = 0; }I want to use aforementioned structure in the QML to edit and pass back to C++. I use QAbstractListModel with the class, which has struct
Exampleas a member. So my model is working well, all parameters are being passed well through data/setData mechanism. One of the roles is connected with theExample.In my QML delegate file I have created appropriate property:
property var currentExample: model.exampleRoleIn the model I have return Example's copy:
... case ExampleRole: { return QVariant::fromValue(item->example()); } ...And since this place everything works unexpected (with different combinations of registerging/declaring
ADCValue, ValuePositionandExample): sometimes I cannot show in console my struct (output just stops to display something after typedefs values), sometimes I cannot modify this object in QML:currentExample.adc = 10
console.log(currentExample) // here it's still 0and so on...
So my question can be a slightly wide, but: What is the correct way to work with typedefs, gadgets in QML correctly?
-
I have found partial "solution":
- QML can not correctly work with
quint8when it was typedefed and qRegistered. Changing initial type to quint32 solved the issue when QML can not correctly display its value. - Passing Q_GADGET to QML via model's method
datawith usingQVariantas a "transport" type works incorrectly (Q_GADGET inner variables cannot be changed for some reason), but passing it directly with separate model's method as
Example getExpample(int index);works well. Anyway copying the Example's object in the both cases works well, but the last one allows to modify Example's members in QML.
I can not say that my issue was solved (now I'm going to use quint32 and the second approach to pass Q_GADGET copy to QML), but I'm wondering, if this is a correct QML/QVariant/Q_GADGET behaviour to avoid such issues further.
- QML can not correctly work with