diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2019-06-06 09:55:20 +0200 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2019-06-11 09:18:49 +0200 |
commit | bf8862a3bfa3010986ed638e90d870fbd2a61435 (patch) | |
tree | 3a35d92336bcec3164d2ebef1af10a6c839f6c08 /src/qml/qml/qqmlmetatype.cpp | |
parent | b0829884c8da99a8ca7d9bd933980fef2527638f (diff) |
add std::function overload to qmlRegisterSingletonType
This changes enables passing stateful lambdas to
qmlRegisterSingletonType, which helps when porting away from
setContextProperty.
Unfortunately, we cannot directly add an overload for std::function, as
this causes ambiguity in the overload set when a lambda of the form
auto f = [](QQmlEngine*, QJSEngine*) -> QObject*
is passed to the function (which is what the examples do)
We therefore use a template to support abribtrary callables f, then SFINAE
them out if f is not convertible to the desired std::function, or when
f is convertible to a plain C function pointer, thus removing the
ambiguity
Change-Id: I6ca95ad692d8bb785e420b85bf3d8c1d0007ce17
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlmetatype.cpp')
-rw-r--r-- | src/qml/qml/qqmlmetatype.cpp | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/qml/qml/qqmlmetatype.cpp b/src/qml/qml/qqmlmetatype.cpp index c6858780a1..a2c2ae47af 100644 --- a/src/qml/qml/qqmlmetatype.cpp +++ b/src/qml/qml/qqmlmetatype.cpp @@ -107,7 +107,7 @@ static QQmlTypePrivate *createQQmlType(QQmlMetaTypeData *data, const QString &el d->version_maj = type.versionMajor; d->version_min = type.versionMinor; - if (type.qobjectApi) { + if (type.qobjectApi || (type.version >= 3 && type.generalizedQobjectApi)) { if (type.version >= 1) // static metaobject added in version 1 d->baseMetaObject = type.instanceMetaObject; if (type.version >= 2) // typeId added in version 2 @@ -118,10 +118,14 @@ static QQmlTypePrivate *createQQmlType(QQmlMetaTypeData *data, const QString &el d->extraData.sd->singletonInstanceInfo = new QQmlType::SingletonInstanceInfo; d->extraData.sd->singletonInstanceInfo->scriptCallback = type.scriptApi; - d->extraData.sd->singletonInstanceInfo->qobjectCallback = type.qobjectApi; + if (type.version >= 3) { + d->extraData.sd->singletonInstanceInfo->qobjectCallback = type.generalizedQobjectApi; + } else { + d->extraData.sd->singletonInstanceInfo->qobjectCallback = type.qobjectApi; + } d->extraData.sd->singletonInstanceInfo->typeName = QString::fromUtf8(type.typeName); d->extraData.sd->singletonInstanceInfo->instanceMetaObject - = (type.qobjectApi && type.version >= 1) ? type.instanceMetaObject : nullptr; + = ((type.qobjectApi || (type.version >= 3 && type.generalizedQobjectApi) ) && type.version >= 1) ? type.instanceMetaObject : nullptr; return d; } |