[Interest] Correct way of exposing enum list to QtQuick

Jérôme Godbout jerome at bodycad.com
Mon Jul 25 15:11:40 CEST 2016


If you have a list of int enum (which you should, manipulate the interger
value and convert to string only to save/restore value and display to user
options):

objectOfMyClass.availaleValuesOfMyEnum.indexOf(parseIInt(objectOfMyClass.myEnumProperty));
// This should work

If you want to have a string value you may consider returning a QVariantMap
instead of QVariantList where the key is the enum and the value is the
string.

QVariantMap rv {{ MyClass::MyEnumValue1: "MyStringValueForEnum1"}, {
MyClass::MyEnumValue2: "MyStringValueForEnum2"}};

You could also make a Q_INVOKABLE getter to avoid pulling the whole map
each time you do a conversion.

note that you can access the enum by string too into Qml:
var my_enum_val = MyClass["MyEnumValue1"];
is just like (you don't need the instance to access the enum value):
var my_enum_val = MyClass.MyEnumValue1;

or setting a class enum for it's own input:

objectOfMyClass
{
  myEnumProperty: "MyEnumValue1" // this work
}

On Mon, Jul 25, 2016 at 8:35 AM, Tomasz Olszak <olszak.tomasz at gmail.com>
wrote:

> Thank you for replies,
>
> Seems like there is no easy way of doing that.
>
> Andre still on Qt Quick side list.indexOf(MyEnum.MyEnumValue1) won't work
> for list being string list.
>
> Cheers,
> Tomasz
>
> 2016-07-22 22:13 GMT+01:00 André Somers <andre at familiesomers.nl>:
>
>>
>>
>> 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 listInterest at qt-project.orghttp://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
> _______________________________________________
> 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/20160725/9eb8bdf0/attachment.html>


More information about the Interest mailing list