Accessing Structure inside a structure in QML
Asked Answered
H

1

0

Previously I posted a question on how to access structures in QML and got perfect answers from some awesome people and now i need to know is there any way to access structure inside a structure in QML, Following is the code :

//MyNewStruct

struct MyNewStruct {
 Q_GADGET
    float m_range;
    Q_PROPERTY(float range MEMBER m_range)
};

//MyStruct

struct MyStruct {
Q_GADGET
int m_val;
QString m_name1;
QString m_name2;
QString m_name3;
QString m_name4;

MyNewStruct m_newStr; //**new Struct declaration

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 as != cannot be used
};
Halliehallman answered 14/8, 2017 at 19:39 Comment(6)
What is the error?Refractive
error: no match for 'operator!=' (operand types are 'myStruct1' and 'myStruct1') if (_t->mynewstr != *reinterpret_cast< myStruct1*>(_v)) { ^ where Mystruct1 is myNewStructHalliehallman
I am no good at C++, but might I ask, why you are using reinterpret_cast? I thought there are almost no cases where this cast is necessary... But as I said, I am just learning C++ right now ;-)Afterglow
@derM I suspect this is moc generated code.Refractive
Ok. :-( Nothing new to learn.Afterglow
@derM this is moc generated error. I am just using the code as above .Halliehallman
H
1

The error which I was getting in MOC was due to operator "!=" functionality was still undefined.

Since these kinds of structure definitions are required when we are building a complex application/module that's where I thought of posting this question here and also there is no proper doc available.

Coming to the question: I used simple operator loading in my struct (since methods are allowed here ) Following is the code:

struct MyNewStruct {
 Q_GADGET
    float m_range;
    Q_PROPERTY(float range MEMBER m_range)

    //Overload operator !=
    bool operator!=(const MyNewStruct & val)
    {
         //Do check with local members and return true/false
    }
};

By doing this I was able to access MyNewStruct in QML.Hope it helps others.

Halliehallman answered 15/8, 2017 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.