[Interest] Accessing existing enums from QML

Tomasz Olszak olszak.tomasz at gmail.com
Wed May 7 21:41:50 CEST 2014


Dnia środa, 7 maja 2014 19:31:58 Jan Kundrát pisze:
> Hi,
> my C++ code makes heavy use of quite simple enums which are defined in some
> namespace, outside of any class (and therefore also outside of any class
> which derives from QObject). It's done like this:
> 
>   namespace Imap {
>     typedef enum { FLAG_ADD, FLAG_REMOVE } FlagsOperation;
>   }
> 
> I also have some classes derived from QObject which accept these enums as
> an argument. This works just fine from C++. I would like to export these
> methods to QML, but I'm having trouble coming up with a scheme which will
> allow my QML code to access these enums.
> 
> The documentation [1] says that "C++ signals and methods with
> enumeration-type parameters can be used from QML provided that the
> enumeration and the signal or method are both declared within the same
> class, or that the enumeration value is one of those declared in the Qt
> Namespace.". That's clearly not the case for these existing methods, and
> the idea of changing our C++ code in order to accommodate a random QML
> requirement looks rather weird.
> 
> What would be the easiest way of making this work? A typedef? Or some hack
> to register that type through qmlRegisterType?
> 
> With kind regards,
> Jan
> 
> [1]
> http://qt-project.org/doc/qt-5/qtqml-cppintegration-data.html#enumeration-ty
> pes-as-signal-and-method-parameters

Currently Qml doesn't handle such case. You can wrap your enums in QtObject 
and expose it as singleton[1] object and use from inside Qml:

import imap
Item {
property int operation: imap.FLAG_ADD
}

[1] http://qt-project.org/doc/qt-5/qqmlengine.html#qmlRegisterSingletonType


-- 
regards, 
Tomasz Olszak




More information about the Interest mailing list