[Qt-qml] enumerations in QML
Alex
alexlordax+qt at gmail.com
Thu Aug 5 21:10:32 CEST 2010
On Wed, Aug 4, 2010 at 10:03 PM, <bea.lam at nokia.com> wrote:
> On 05/08/2010, at 2:45 PM, ext Gregory Schlomoff wrote:
>
>> Isn't there a way to expose an enum to QML with qRegisterType or something?
>>
>
> If an enum is exposed to Qt's Meta Object system using Q_ENUMS, the enum is automatically available to QML.
>
> This is mentioned in http://doc.qt.nokia.com/4.7-snapshot/declarative-tutorials-extending-chapter4-custompropertytypes.html (but probably should be moved to some other reference documentation).
>
>
Many thanks to all who responded.
I agree with Gregory about weak-typing being an impediment to
designing reusable and configurable components. I also happened upon
the trick he mentioned -- using integer properties to represent
possible values symbolically. However, this does not prevent misuse.
First, if the symbolic properties are not read-only (const), their
values could accidentally be modified. Second, nothing prevents a
plain integer from being used instead of a symbolic name.
Bea's suggestion regarding Q_ENUMS is useful, but it breaks
encapsulation in those cases where pure QML components need to expose
enums.
Since the QML API itself uses enums, I think it would be more
symmetrical to support the feature directly within the QML language,
regardless of javascript/C++ bindings.
Cheers,
Alex
More information about the Qt-qml
mailing list