qtqmlqtquick2qgadget

Accessing Structure inside a structure in QML


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
};

Solution

  • 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.