简体   繁体   English

如何从 QML 访问在 C++ class 中声明的 Q_ENUM?

[英]How to access a Q_ENUM declared in C++ class from QML?

I have a Q_ENUM declared in MyClass like below:我在MyClass中声明了一个Q_ENUM ,如下所示:

class MyClass {
public:
    enum Enum_Test {
        eTestA,
        eTestB
    }
    Q_ENUM(Enum_Test)

    Q_OBJECT
    Q_PROPERTY(MyClass::Enum_Test enumTest READ GetEnumTest WRITE SetEnumTest )
}

I have MyClass registered on the QML side like below and able to access it.我在 QML 端注册了MyClass ,如下所示,并且能够访问它。

auto my_class = std::make_shared<MyClass>();
qmlRegisterUncreatableType<MyClass>("MyClass", 1,0, "MyClass","Cannot create type MyClass in QML");
rootContext()->setContextProperty("my_class", my_class.get());

How do I access Q_ENUM Enum_Test from QML?如何从 QML 访问Q_ENUM Enum_Test Enum_Test?

Your class needs two adjustments.您的 class 需要两次调整。

  1. as pointed out by JarMan, it needs a metaObject, which can be obtained by deriving from QObject and adding Q_OBJECT:正如JarMan所指出的,它需要一个元对象,可以通过从QObject派生并添加Q_OBJECT来获得:

     class MyClass: public QObject { Q_Object... }

    Actually, it would also be possible to use Q_GADGET but you already seem to lean towards Q_OBJECT .实际上,也可以使用Q_GADGET ,但您似乎已经倾向于Q_OBJECT But, as requested, here we go:但是,根据要求,我们在这里 go:

    class MyClass { Q_GADGET class MyClass { Q_GADGET

     public: enum Enum_Test { ETestA, ETestB } Q_ENUM(Enum_Test)

    }; };

    Keep in mind that Q_GADGET cannot have signals , so I left out the property and only have this class as "enum-placeholder".请记住,Q_GADGET不能有信号,所以我省略了该属性,只将这个 class 作为“枚举占位符”。

  2. The enum value names need to be capitalized:枚举值名称需要大写:

     enum Enum_Test { ETestA, ETestB } Q_ENUM(Enum_Test)

Then you can use it in QML as:然后你可以在 QML 中使用它:

     QtObject {
         property int myEnumVal: MyClass.ETestA
     }

Note that support for enums is somewhat limited because the mix with JavaScript.请注意,对枚举的支持有些有限,因为它与 JavaScript 混合使用。 The values will be converted to integers.这些值将被转换为整数。 Also, when used in a JavaScript switch-statement, typo's will not be warned about by QtCreator (assuming version 4.14)此外,当在 JavaScript 开关语句中使用时,QtCreator 不会警告错字(假设版本 4.14)

The answer provided by @Amfasis is already good. @Amfasis 提供的答案已经很好了。 As I tend to do this pretty often, I wrote a little shortcut for defining enums with all the features Qt offers, including the availability in QML here: https://github.com/carlonluca/lqtutils#lqtutils_enumh .因为我经常这样做,所以我写了一个小快捷方式来定义枚举,它具有 Qt 提供的所有功能,包括 QML 中的可用性: httpsgithub://aenqutils.com/carlon

You only need to include the header, define your enum values like:您只需要包含 header,定义您的枚举值,如:

L_DECLARE_ENUM(Enum_Test,
    ETestA,
    ETestB
)

and register it where you prefer with:并在您喜欢的地方注册:

Enum_Test::qmlRegisterMySharedEnum("some.uri", 1, 0);

Also this uses simple namespaces, which is lighter than using QObjects or gadgets.这也使用了简单的命名空间,这比使用 QObjects 或小工具更轻。

Actually, I got used to always declare my enums like this when I'm using Qt, because I can also benefit from QMetaEnum .实际上,当我使用 Qt 时,我习惯于总是这样声明我的枚举,因为我也可以从QMetaEnum中受益。 For instance, I really like to be able to log like this:例如,我真的很喜欢能够像这样记录:

qDebug() << "Value:" << Enum_Test::ETestA;

getting:得到:

Value: Enum_Test::ETestA

instead of a simple integer.而不是简单的 integer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM