在QML中访问结构内部的结构 [英] Accessing Structure inside a structure in QML
问题描述
以前,我发布了一个有关如何在QML中访问结构的问题,并从一些很棒的人那里得到了完美的答案,现在我需要知道有什么方法可以访问中的结构内的结构. 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
};
推荐答案
我在MOC中遇到的错误是由于运算符"!= "功能仍未定义.
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
}
};
这样做可以访问QML中的MyNewStruct.希望对其他人有帮助.
By doing this I was able to access MyNewStruct in QML.Hope it helps others.
这篇关于在QML中访问结构内部的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!