将Qt的Q_ENUMS公开给QML [英] Exposing Qt's Q_ENUMS to QML
问题描述
/ p>
class MyClass:public QDeclarativeItem {
Q_OBJECT
Q_ENUMS(testType)
public:
MyClass():t(FirstValue){}
枚举testType {InvalidValue,FirstValue,SecondValue};
testType testVal()const {return t; }
Q_PROPERTY(testType testVal READ testVal NOTIFY testValChanged)
private:
testType t;
信号:
void testValChanged();
};
然后我注册&将这个类的实例注入到QDeclartiveContext中。
当我尝试访问testVal属性时,它返回整数(在这种情况下为1),而不是一个字符串表示形式。
另外,实例注入'aVar',如果我尝试访问'aVar.FirstValue',结果是'undefined'
所以这样意味着我无法像以下测试:'如果aVar.testVal == FirstValue'
(不合格的FirstValue的ReferenceError)
或者像这样:'如果aVar.testVal == aVar.FirstValue'
( aVar.FirstValue
未定义)
以前有人通过了吗?这似乎与QT文档中提供的示例有冲突,但是在该示例中,该对象是从QML实例化的,因此可能是原因。
此外,枚举值不作为字符串返回,因为它们被定义为整数值。
I might be missing something obvious here, but when trying to expose a Q_ENUM to QML, even in the most simple case, does not seem to work as shown in the QT docs (http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#using-enumerations-of-a-custom-type)
I've created a simple test case, my C++ class looks like:
class MyClass : public QDeclarativeItem {
Q_OBJECT
Q_ENUMS(testType)
public:
MyClass() : t(FirstValue) { }
enum testType { InvalidValue, FirstValue, SecondValue } ;
testType testVal() const { return t; }
Q_PROPERTY(testType testVal READ testVal NOTIFY testValChanged)
private:
testType t;
signals:
void testValChanged();
};
I then register & inject an instance of this class into the QDeclartiveContext.
When I try and access the testVal property, it returns the integer (1 in this case) rather than a string representation. In addition, with the instance injected as 'aVar', if I try and access 'aVar.FirstValue', the result is 'undefined'
So this means I can't do tests like: 'if aVar.testVal == FirstValue'
(ReferenceError for the unqualified FirstValue)
Or like this: 'if aVar.testVal == aVar.FirstValue'
(aVar.FirstValue
is undefined)
Anyone been through this before? It seems to conflict with example provided in the QT docs, although, the Object is instantiated from QML in that example, so this might be the cause..
Enum values can only be accessed as "ElementName.EnumValue", not "object.EnumValue". So, aVar.FirstValue won't work; you'll need to use MyClass.FirstValue instead (and to do this, you'll need to register MyClass with qmlRegisterType() and then import the registered module).
Also, enum values as not returned as strings since they are defined as integer values.
这篇关于将Qt的Q_ENUMS公开给QML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!