[Interest] Use of enum class, Q_ENUM and QML

Ola Røer Thorsen ola at silentwings.no
Thu Apr 9 11:14:06 CEST 2015


Hi,

I'm looking into using enum classes (c++11) and exposing them to QML as a
part of planning a new project. It will use Qt 5.5 so I'm using Qt 5.5
alpha for now.

I'd like to use enum classes for the enums, but I'm not sure if it is
possible to use these in QML. Let's say I have two enums in a class,

TestClass
{
   Q_GADGET

public:
   enum class Something { Undef, On, Off };
   Q_ENUM(Something)
   enum class Other { Undef = 5, On = 6, Off = 10, Maybe = 255 };
   Q_ENUM(Other)
};

In my main.cpp I register this like so

qmlRegisterUncreatableType<TestClass>("Test",1,0,"Test","Error...");

There does not seem to be any way in QML to differ between the two enum
classes. Looks like the values of the second defined class are used (so in
QML, Test.Undef will have the value of 5).

I tried registering the enum classes, but that doesn't work
(qmlRegisterUncreatableType<TestClass::Something> ... )

Do any of you know a way around this? The obvious fallback is to use unique
enum keys like when not using enum classes, (SomethingUndef, SomethingOn,
etc..), but I'd like not to, if it can be avoided.

Cheers,
Ola
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150409/a1d8a4c4/attachment.html>


More information about the Interest mailing list