[Interest] Correct way of exposing enum list to QtQuick

André Somers andre at familiesomers.nl
Fri Jul 22 23:13:58 CEST 2016



Op 21/07/2016 om 17:17 schreef Tomasz Olszak:
> Hi,
>
> I encountered something that bothers me for a few days. I would be 
> thankful if someone can point nice workaround for such case:
>
> class MyClass {
>      Q_OBJECT
>      Q_PROPERTY(MyEnum myEnumProperty ...)
>      Q_PROPERTY(QVariantList availableValuesOfMyEnum ...)
> public:
>     enum MyEnum {
>       MyEnumValue1,
>       MyEnumValue1
>     }
>     Q_ENUM(MyEnum)
> }
>
> From Qt Quick everything works ok except indexOf:
>
> objectOfMyClass.availaleValuesOfMyEnum.indexOf(objectOfMyClass.myEnumProperty) 
> gives -1
>
> And it is reasonable because availaleValuesOfMyEnum is list of 
> QVariant not list of MyEnum.
>
> If I change type of myEnumProperty to QVariant, indexOf works ok, but 
> javascript switch doesn't (QVariant is not int). Additionally 
> myEnumProperty is printed nicely in logs from Qt Quick(not raw integer 
> value).
>
> Question:
> Does anyone has better idea how to expose list of enums to QtQuick so 
> that searching for enum in such list will work out of the box?
>
> Cheers,
> Tomek
>
You could consider sharing the values as strings instead. With Q_ENUM, 
it is easy to turn the enums into strings. And you can search for a 
string I think.

André

>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160722/cfe0dc0f/attachment.html>


More information about the Interest mailing list