我可能会遗漏一些明显的东西,但是当试图将Q_ENUM暴露给QML时,即使是在最简单的情况下,也似乎没有像QT文档中那样工作(http://doc.qt.nokia.com/4.7 -snapshot/qtbinding.html#using-enumerations-of-a-custom-type)
我创建了一个简单的测试用例,我的C++类看起来像:
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();
};
Run Code Online (Sandbox Code Playgroud)
然后我注册并将此类的实例注入QDeclartiveContext.
当我尝试访问testVal属性时,它返回整数(在本例中为1)而不是字符串表示.另外,如果实例注入'aVar',如果我尝试访问'aVar.FirstValue',结果是'undefined'
所以这意味着我不能做以下测试:( 'if aVar.testVal == FirstValue' 不合格的FirstValue的ReferenceError)
或者是这样的:'if aVar.testVal == aVar.FirstValue'(aVar.FirstValue未定义)
以前有人经历过这个吗?它似乎与QT文档中提供的示例冲突,但是,在该示例中,Object是从QML实例化的,因此这可能是原因..