[Development] API style guide: scoped enum or not?

Fabian Kosmale fabian.kosmale at qt.io
Tue Jun 20 16:36:44 CEST 2023


I'm technically not a maintainer of either QtQml or QtQuick, but I think 
I'm qualified to chime in here:

QML has (slightly hidden in 
https://doc.qt.io/qt-6/qtqml-cppintegration-data.html#enumeration-types) 
a way to disable this behavior by setting

Q_CLASSINFO("RegisterEnumClassesUnscoped", "false")

If we had a time machine, that would certainly been a good default. 
However, toggling that behavior (or unconditionally enforcing it) would 
have a massive potential of breaking code; and currently you would only 
notice that at runtime.

What we can do in Qt 6 is to add a check in qmllint to catch scoped 
enums that are exposed to QML without setting 
RegisterEnumClassesUnscoped to false; and warn about them. Maybe even 
add a fixit to clazy/clang-tidy/other tool to convert this, and some 
additional tooling to update the QML files.

We can also add that Q_CLASSINFO for new API exposed to QML, so that 
there are no issues in that area.

And if we get this in place relatively soon, we can consider changing 
the default in Qt 7 (and removing support for accessing enums in an 
unscoped way in Qt 8).

But changing the current behavior of QML is an absolute no-go.

Regards,
Fabian

On 20.06.23 15:55, Volker Hilsheimer via Development wrote:
>> On 14 Jun 2023, at 14:59, Marc Mutz via Development <development at qt-project.org> wrote:
> 
> […]
> 
>> C) scoped enums SHOULD NOT repeat (part of) an enum's type name in the
>> enumerators²
> 
> […]
> 
>> I have been told today that QML allows scoped C++ enums to be used
>> without the scope. If this is true, it should be fixed to always require
>> the scope, because otherwise the argument goes "but in QML, the scope
>> isn't visible, so a part of the enum type name must be included in the
>> enumerators”.
> 
> What’s the view from the QML maintainers on this particular point?
> 
> That QML allows using an enum value without scope if the C++ enum is scoped seems like a bug to me, although I assume that changing this will come with some incompatibility fallout.
> 
> Volker
> 

-- 
Fabian Kosmale
Manager R&D

The Qt Company GmbH
Erich-Thilo-Str. 10
D-12489 Berlin
fabian.kosmale at qt.io
+49 1638686070


More information about the Development mailing list