[Interest] Accessing existing enums from QML

Tomasz Olszak olszak.tomasz at gmail.com
Tue May 13 20:44:34 CEST 2014

Dnia wtorek, 13 maja 2014 17:30:18 Jan Kundrát pisze:
> On Wednesday, 7 May 2014 21:41:50 CEST, Tomasz Olszak wrote:
> > 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
> > }
> Do you have a small example on how to do just that? Again, the problem is
> that the original enum is defined *outside* of any QObject. In principle, I
> could change this, but for clarity I would prefer to do something like a
> typedef. Unfortunately, typedefs doesn't really work, and the best approach
> that we were able to come up with is something ugly like this:
> namespace Original {
>   enum Foo { A, B };
> }
> namespace QmlSupport {
> class Whatever : public QObject
> {
>   Q_PROPERTY(Foo foo MEMBER m_foo)
> public:
>   enum Foo {
>     A = Original::A,
>     B = Original::B,
>   }
> }
> }
> ...and having to duplicate my enums like this is rather suboptimal.
Yes, but this boilerplate is easy to remove when direct exposition of enum to 
Qml will be implemented.

> Using the typedef to prepare a new name for an older enum, it appears that
> I can only use Q_PROPERTY with the new name which refers to an enum inside
> some QObject. I can only have one Q_DECLARE_METATYPE, either for
> Original::Foo or the QmlSupport::Whatever::Foo. When I
> qRegisterMetaType<QmlSupport::Whatever::Foo>, I still get "undefined" when
> I try to console.log(whatever.A) or console.log(whatever.foo).
> Or is moving the enums into some QObject my only chance, actually?
AFAIK  yes.

I've prepared you an example and removed the singleton part - 
qmlRegisterUncreatableType is more neat.

You can download tarball [1]. This is as far as I can get in reasonable amount 
of time. It looks like almost all cases that came to my mind works except 
invoking slot with enum argument from other class (registered as uncreatable). 

I also made a simple snippet which is example[2] of not working slot. Could 
someone determine if it is a bug and needs to be submitted?

[1] https://drive.google.com/file/d/0Bwa77UXf_zhhcGg5YzM3YjhSbjA/edit?pli=1
[2] http://pastebin.com/XxQvdrny
Tomasz Olszak

More information about the Interest mailing list