[Development] Enumerations in QML

Verbruggen Erik Erik.Verbruggen at digia.com
Wed Dec 12 10:45:02 CET 2012


On Dec 11, 2012, at 4:47, 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?

And, also important: how much slower is it allowed to be than the current implementation?

I remembered that there was talk about enums in QML before, so I dug around a bit, and behold:

The problem with enums is that we couldn't make them fast. They will always be a lot slower than normal string literals. That is mostly because we could not avoid the name lookup. Think about,

   Text {
           alignment: Alignment.Vertical | Alignment.something
    }

there is no way for you to remove the name lookups for "Alignment", "Vertical", and "something". Also, there is no way for you to distinguish "Types" (e.g. Item, Rectangle, and co...) from "Enums" (in our case "Alignment"). That means, you cannot build the correct list of dependencies when loading a QML Component.

-- Erik.




More information about the Development mailing list