diff options
author | Rainer Keller <Rainer.Keller@qt.io> | 2018-08-20 13:48:37 +0200 |
---|---|---|
committer | Rainer Keller <Rainer.Keller@qt.io> | 2018-08-21 11:43:36 +0000 |
commit | bb296168f426d7e646c0208427f25687f96e2693 (patch) | |
tree | f1683300d7b2aac946de627a87f8e69681a54b99 /src/qml/qml/qqmlmetatype.cpp | |
parent | b858833a262aeef005e4e3533194db7e6c280c4c (diff) |
Provide option to skip registration of enum classes unscoped
Enums classes are registered unscoped which leads to clashes in the following
cases:
1. Two different enums contain the same values
2. The name of an enum class is the same as an enum value
In the 2nd case you can not even access the scoped enum at all because it
will be overwritten by the primitive type.
Users can now add a class info to the meta type to disable the unscoped
registration which solves all clashes. The default is kept as is.
class MyClass : public QObject
{
Q_OBJECT
Q_CLASSINFO("RegisterEnumClassesUnscoped", "false")
public:
...
};
[ChangeLog][QtQml] Added option to disable unscoped registration of enum classes.
Change-Id: Ifa4197a14a252575e8a25ae56fb6ee479addf80b
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlmetatype.cpp')
-rw-r--r-- | src/qml/qml/qqmlmetatype.cpp | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/qml/qml/qqmlmetatype.cpp b/src/qml/qml/qqmlmetatype.cpp index ed43d1829c..75241ab99c 100644 --- a/src/qml/qml/qqmlmetatype.cpp +++ b/src/qml/qml/qqmlmetatype.cpp @@ -218,6 +218,7 @@ public: int attachedPropertiesId; int propertyValueSourceCast; int propertyValueInterceptorCast; + bool registerEnumClassesUnscoped; }; struct QQmlSingletonTypeData @@ -369,6 +370,7 @@ QQmlTypePrivate::QQmlTypePrivate(QQmlType::RegistrationType type) extraData.cd->attachedPropertiesType = nullptr; extraData.cd->propertyValueSourceCast = -1; extraData.cd->propertyValueInterceptorCast = -1; + extraData.cd->registerEnumClassesUnscoped = true; break; case QQmlType::SingletonType: case QQmlType::CompositeSingletonType: @@ -495,9 +497,15 @@ QQmlType::QQmlType(QQmlMetaTypeData *data, const QString &elementName, const QQm d->extraData.cd->propertyValueInterceptorCast = type.valueInterceptorCast; d->extraData.cd->extFunc = type.extensionObjectCreate; d->extraData.cd->customParser = type.customParser; + d->extraData.cd->registerEnumClassesUnscoped = true; if (type.extensionMetaObject) d->extraData.cd->extMetaObject = type.extensionMetaObject; + + // Check if the user wants only scoped enum classes + auto indexOfClassInfo = metaObject()->indexOfClassInfo("RegisterEnumClassesUnscoped"); + if (indexOfClassInfo != -1 && QString::fromUtf8(metaObject()->classInfo(indexOfClassInfo).value()) == QLatin1String("false")) + d->extraData.cd->registerEnumClassesUnscoped = false; } QQmlType::QQmlType(QQmlMetaTypeData *data, const QString &elementName, const QQmlPrivate::RegisterCompositeType &type) @@ -834,7 +842,11 @@ void QQmlTypePrivate::insertEnums(const QMetaObject *metaObject) const for (int jj = 0; jj < e.keyCount(); ++jj) { const QString key = QString::fromUtf8(e.key(jj)); const int value = e.value(jj); - enums.insert(key, value); + if (!isScoped || (regType == QQmlType::CppType && extraData.cd->registerEnumClassesUnscoped)) { + if (enums.contains(key)) + qWarning("Previously registered enum will be overwritten due to name clash: %s.%s", metaObject->className(), key.toUtf8().constData()); + enums.insert(key, value); + } if (isScoped) scoped->insert(key, value); } |