Solved Best way to access a cpp structure in QML
-
@Wieland Thanks !! for pointing that .
-
@Wieland Is there any possibility that I can access structure inside a structure?
struct MyStruct { Q_GADGET int m_val; QString m_name1; QString m_name2; QString m_name3; QString m_name4; MyNewStruct m_newStr; //** new Struct includes Q_GADGET macro and member definations. Q_PROPERTY(int val MEMBER m_val) Q_PROPERTY(QString name1 MEMBER m_name1) Q_PROPERTY(QString name2 MEMBER m_name2) Q_PROPERTY(QString name3 MEMBER m_name3) Q_PROPERTY(QString name4 MEMBER m_name4) Q_PROPERTY(MyNewStruct newStr MEMBER m_newStr) //**Currently getting error };
-
-
This post is deleted! -
@Wieland
error: no match for 'operator!=' (operand types are 'myStruct1' and 'myStruct1') if (_t->mynewstr != *reinterpret_cast< myStruct1*>(_v)) {
where Mystruct1 is myNewStruct and the error is in MOC ...
-
The MOC generates some code for you to implement all the Gadget / Property stuff. Looks like the generated code uses the != operator for MyStruct. So you need to implement that:
public: bool operator==(MyStruct const &other) const; bool operator!=(MyStruct const &other) const;
bool MyStruct::operator==(const MyStruct &other) const { // compare members return true; } bool MyStruct::operator!=(MyStruct const &other) const { return !(*this == other); }
-
@Wieland That worked !!! Thanks, How to know that which all operators should be overloaded?
-
@pra7 said in Best way to access a cpp structure in QML:
How to know that which all operators should be overloaded?
You can't really know. But when the compiler complains about a missing operator, just implement it. In this case we didn't really need the
==
operator, but I'd say it's common practice to implement the!=
operator using the==
operator. -
@Wieland Thanks for all your suggestions.
-
@pra7, @Wieland how can I access a list of Structure inside another Structure.
Eg: From the above example, I need to useQList<MyNewStructure> m_StructList; How is that possible? I have tried, QList<MyNewStructure> m_StructList; Q_PROPERTY(QList<MyNewStruct> newStr MEMBER m_newStr) but didnt help.
-
@pra7 but it does not sufficient! I had to use
Q_DECLARE_METATYPE()
andqRegisterMetaType<>()
to avoidunknown type
error. Did I missed something? -
-