[Development] [QML] Assigning ints to enumeration properties

Alan Alpert 416365416c at gmail.com
Thu Mar 7 17:45:27 CET 2013


On Wed, Mar 6, 2013 at 11:58 PM, Knoll Lars <Lars.Knoll at digia.com> wrote:
>
>
> On 3/6/13 8:23 PM, "Alan Alpert" <416365416c at gmail.com> wrote:
>
>>On Wed, Mar 6, 2013 at 3:11 AM, Laszlo Papp <lpapp at kde.org> wrote:
>>> On Wed, Mar 6, 2013 at 9:48 AM, Olivier Goffart <olivier at woboq.com>
>>>wrote:
>>>>
>>>> This is not a warning, this is an error in C++
>>>>
>>>> enum Foo { A ,  B };
>>>> Foo bar = 1; // error: invalid conversion from Œint¹ to ŒFoo¹
>>>> int blah = bar; // No warning. unless you use enum class then it is a
>>>> error.
>>>
>>>
>>> At least, I can second that even with msvc variants, but perhaps Alan
>>>meant
>>> -fpermissive or so.
>>
>>Yes - although I thought -fpermissive was on by default for gcc (but
>>it isn't, now that I've checked). Whoops.
>>
>>>
>>>> I think implicit casts is *not* a good idea.
>>>>
>>>> Allowing explicit casts should be good.
>>>
>>>
>>> That would be a bit uglier in QML than a static or function type
>>>explicit
>>> cast like in C++ unless there is a nice trick not to make map, object,
>>> mapper function or so for enums with many values. Perhaps parseInt() or
>>> something similar? I would not know.
>>>
>>> If there is no nice trick, I would suggest allowing it (perhaps with a
>>> warning on the console?) because I do not wish to maintain such code if
>>>I
>>> know what I am doing. :)
>>
>>Unfortunately neither explicit casts nor a warning on the console
>>would work well.
>
> Why wouldn't explicit casts work? I can understand limitations in the
> engine making this difficult, but in principle a syntax similar to JS
> constructor objects should be doable. Like that we could even get the same
> syntax as on the C++ side:
>
> enumProperty: MyEnumType(myInt)

I didn't mean that it was a technical problem, I mean that it seems
odd to have a cast function which would only be used in simple
property assignments.

horizontalAlignment: Qt.Alignment(1)
horizontalAlignment: 1

The extra cast seems unnecessary to me, and inconsistent with JS style
conversions (like toString()).

I could see the casts making more sense if we actually checked the
type of the enum (which also requires the cast so you can convert
between enums). So long term, QML 3, that could be a solution. I'd be
happy to leave the enums at they are if that long term solution is
liked, but if we're not waiting for that then allowing ints now makes
enums more usable.

--
Alan Alpert



More information about the Development mailing list