[Qt-qml] enumerations in QML
Alan Alpert
alan.alpert at nokia.com
Thu Aug 5 06:11:05 CEST 2010
On Thu, 5 Aug 2010 10:36:29 ext Alex wrote:
> Is there any way to declare enumerations in QML? For example, assume I
> have a custom element called "MyText" that has a property called
> sizeType, for which the possible values are: "small", "medium", or
> "big."
>
> The implementation of MyText will set the correct point or pixel size
> based on the value of sizeType. Is there a way to expose the possible
> values using an enum (e.g., MyText::SizeBig) rather than something
> more primitive like int or string?
Not from QML. From what I understand, since Javascript is a weakly typed
language anyways, the appropriate Javascript way is to just use a string
(which has valid values of "small", "medium" and "large", and a specified
behavior for invalid values).
> Similarly, what is the best way to declare constants?
Again my understanding of Javascript suggests that you create constants by
having variables that you ask people not to set. Same for when using
Javascript in QML.
These concepts make more sense in a statically typed language like C++ than in
a dynamically typed language like Javascript. So, while it's acceptable to let
the terms leak in from C++ due to the tight C++ integration in QML, I don't
think you should worry about them too much when writing pure QML. If you want
very concrete elements, perhaps you should write them in C++.
--
Alan Alpert
Software Engineer
Nokia, Qt Development Frameworks
More information about the Qt-qml
mailing list