[Qt-qml] enumerations in QML
Gregory Schlomoff
gregory.schlomoff at gmail.com
Thu Aug 5 06:52:53 CEST 2010
Edit: a work around that I just found:
=== MyScrollbar.qml ==
Rectangle {
property int orientation: undefined
property alias scrollbar: scrollbar
QtObject { // this is my "enum" :)
id: scrollbar
property int vertical: 1
property int horizontal: 2
}
[...]
}
=== app.qml ===
MyScrollbar {
orientation: scrollbar.vertical
}
On Thu, Aug 5, 2010 at 11:45 AM, Gregory Schlomoff
<gregory.schlomoff at gmail.com> wrote:
> Isn't there a way to expose an enum to QML with qRegisterType or something?
>
> How do you guys expose enumerations to QML?
>
> While I understand that Javascript is weakly typed and hasn't such
> concept as constants, when you are developing generic components, it's
> very scary to do stuff like that:
>
> MyScrollbar {
> orientation: "vertical"
> }
>
> You just know that one day someone is going to mispell "vertical" or
> "horizontal", and the guy is going to have a hard time debugging the
> issue.
>
> In this case, I worked around the problem by reusing a Qt global enum,
> but that doesn't always work:
>
> MyScrollbar {
> orientation: Qt.Vertical //much better :)
> }
>
>
>
> On Thu, Aug 5, 2010 at 11:11 AM, Alan Alpert <alan.alpert at nokia.com> wrote:
>> 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
>> _______________________________________________
>> Qt-qml mailing list
>> Qt-qml at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-qml
>>
>
More information about the Qt-qml
mailing list