将Qt的Q_ENUMS公开给QML [英] Exposing Qt's Q_ENUMS to QML

查看:200
本文介绍了将Qt的Q_ENUMS公开给QML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能在这里缺少一些明显的东西,但是当试图向QML公开Q_ENUM时,即使在最简单的情况下,似乎也没有像QT文档( http://doc.qt.nokia.com/4.7-snapshot/qtbinding我已经创建了一个简单的测试用例,我的C ++类看起来像:



/ 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实例化的,因此可能是原因。

解决方案枚举值只能作为ElementName.EnumValue而不是object.EnumValue访问。所以,aVar.FirstValue将无法正常工作;您将需要使用MyClass.FirstValue(并且为此,您需要使用qmlRegisterType()注册MyClass,然后导入已注册的模块)。



此外,枚举值不作为字符串返回,因为它们被定义为整数值。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆