[Development] Enumerations in QML

Mark markg85 at gmail.com
Tue Dec 11 09:26:20 CET 2012

On Tue, Dec 11, 2012 at 4:47 AM, Alan Alpert <416365416c at gmail.com> wrote:
> People keep asking for enumerations in QML. See QTBUG-15483 and
> QTBUG-14861, both assigned to old Nokia identities (so don't trust
> that 'in progress' ;) ). Now I don't know when these issues will be
> resolved, but there's an important discussion to have before it can
> even be scheduled: What would proper enum support look like in QML?
> QTBUG-15483 suggests 'property Bar::Weather weather: Bar.Sunny', for
> using the C++ 'enum Weather { Raining, Sunny, Cloudy }'. But
> QTBUG-14861 does not include a suggestion of what the QML syntax for
> declaring an enum should be (just a suggestion for how we could hack
> it in there). It's totally not obvious to me what a good QML API for
> declaring enums would be, and that could have run-on effects on how
> they're used in property declarations. All we know is that Bar.Sunny
> is how you use the enum exposed from C++, and that will need to
> continue to work with QML defined enums. Note that "property
> Bar::Weather weather" is just a suggestion as well, I actually suspect
> "property Bar.Weather weather" would fit in better (but it depends on
> how you declare them).
> What should enumeration declarations in QML look like?
> --
> Alan Alpert
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development

I found myself in the exact same situation where it would've been
really convenient to have enums in QML. However, one thing that is
easy to forget is that you still have the full power of JavaScript at
your disposal. And while JavaScript doesn't support enums as those
that you have in C/C++, you still can have it's syntax.

Look at this link: http://stackoverflow.com/questions/287903/enums-in-javascript

The thing i'm using in my project is:

var SIZE = {
  SMALL : 0,
  MEDIUM: 1,
  LARGE : 2

and i'm "somewhat" sure that you can also do that in a qml property
with something like this:
property variant SIZE: {
  SMALL : 0,
  MEDIUM: 1,
  LARGE : 2

Not tested so it probably doesn't work, but you get the idea.
The only downside is that QML doesn't allow the first character (the
"S" of "SIZE) to be written in capital letters and you also assign the
property as QVariant.. It's not ideal but works just fine.

Also, another possibility is adding a separate JavaScript file in
which you just have the var as in my first example. Works like a charm
in my case :) Take a look at my example where i use FontAwesome in my
QML code where i call it as JsUtil.FA.<the_icon_i_want> [1, 2, 3]
[1] http://gitorious.org/porpoise/master/blobs/master/qml/Porpoise/main.qml
[2] http://gitorious.org/porpoise/master/blobs/master/qml/Porpoise/javascript/util.js
[3] http://gitorious.org/porpoise/master/blobs/master/qml/Porpoise/javascript/fontawesome.js

More information about the Development mailing list