[Interest] Accessing existing enums from QML

Jan Kundr√°t jkt at flaska.net
Tue May 13 17:30:18 CEST 2014

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 
> 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)
  enum Foo {
    A = Original::A,
    B = Original::B,


...and having to duplicate my enums like this is rather suboptimal.

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?

With kind regards,

Trojit√°, a fast Qt IMAP e-mail client -- http://trojita.flaska.net/

More information about the Interest mailing list