将 Qt 的 Q_ENUMS 暴露给 QML [英] Exposing Qt's Q_ENUMS to QML

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

问题描述

我可能在这里遗漏了一些明显的东西,但是当尝试将 Q_ENUM 公开给 QML 时,即使在最简单的情况下,似乎也不能像 QT 文档中所示的那样工作(http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#using-enumerations-of-a-custom-type)

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)

我创建了一个简单的测试用例,我的 C++ 类如下所示:

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

然后我注册 &将此类的一个实例注入到 QDeclartiveContext 中.

I then register & inject an instance of this class into the QDeclartiveContext.

当我尝试访问 testVal 属性时,它返回整数(在本例中为 1)而不是字符串表示形式.此外,将实例注入为aVar",如果我尝试访问aVar.FirstValue",结果为未定义"

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'

所以这意味着我不能进行如下测试:'if aVar.testVal == FirstValue'(不合格的 FirstValue 的 ReferenceError)

So this means I can't do tests like: 'if aVar.testVal == FirstValue' (ReferenceError for the unqualified FirstValue)

或者像这样:'if aVar.testVal == aVar.FirstValue'(aVar.FirstValue 未定义)

Or like this: 'if aVar.testVal == aVar.FirstValue' (aVar.FirstValue is undefined)

以前有人经历过吗?它似乎与 QT 文档中提供的示例冲突,尽管在该示例中 Object 是从 QML 实例化的,所以这可能是原因..

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

推荐答案

枚举值只能作为ElementName.EnumValue"访问,不能作为object.EnumValue"访问.所以,aVar.FirstValue 不起作用;您需要改用 MyClass.FirstValue(为此,您需要使用 qmlRegisterType() 注册 MyClass,然后导入注册的模块).

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天全站免登陆