diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-09-08 14:41:42 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2022-09-24 07:54:29 +0000 |
commit | 3195b44e1c9678584c05ed823aab2eb32518d868 (patch) | |
tree | 7c13d0ee0b6f9074841862e160d19d744daeda58 /src/qml/qml/qqml.cpp | |
parent | b101be9be64b6cc82dc357da0faeffbaab771b8f (diff) |
Allow more options for creating value types from JS objects
We allow value types to be created
1. by calling Q_INVOKABLE constructors
2. by setting their values from properties of a JS object
Both have to be opted into by setting a class info. If opted into, these
options override the existing methods. When a a type can be created by
setting its properties, that implies you can also initialize it using an
invokable constructor. However, when given a JS object, the properties
method is used.
We keep this internal and undocumented for now. As the last try (the
create(QJSValue) methods and QJSValue ctors) was not that stellar, let's
first wait a bit and see if we're getting it right this time around.
Fixes: QTBUG-106480
Change-Id: I767230924afcba032d501846cc3263dad57b7bf0
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/qml/qml/qqml.cpp')
-rw-r--r-- | src/qml/qml/qqml.cpp | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/qml/qml/qqml.cpp b/src/qml/qml/qqml.cpp index 81416b6a0d..4b7c21e3ac 100644 --- a/src/qml/qml/qqml.cpp +++ b/src/qml/qml/qqml.cpp @@ -166,7 +166,8 @@ int qmlRegisterUncreatableMetaObject(const QMetaObject &staticMetaObject, nullptr, QTypeRevision::zero(), - -1 + -1, + QQmlPrivate::ValueTypeCreationMethod::None }; return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type); @@ -464,11 +465,19 @@ int QQmlPrivate::qmlregister(RegistrationType type, void *data) && boolClassInfo(type.classInfoMetaObject, "QML.Creatable", true); QString noCreateReason; + ValueTypeCreationMethod creationMethod = ValueTypeCreationMethod::None; if (!creatable) { - noCreateReason = QString::fromUtf8(classInfo(type.classInfoMetaObject, "QML.UncreatableReason")); + noCreateReason = QString::fromUtf8( + classInfo(type.classInfoMetaObject, "QML.UncreatableReason")); if (noCreateReason.isEmpty()) noCreateReason = QLatin1String("Type cannot be created in QML."); + } else if (!(type.typeId.flags() & QMetaType::PointerToQObject)) { + const char *method = classInfo(type.classInfoMetaObject, "QML.CreationMethod"); + if (qstrcmp(method, "structured") == 0) + creationMethod = ValueTypeCreationMethod::Structured; + else if (qstrcmp(method, "construct") == 0) + creationMethod = ValueTypeCreationMethod::Construct; } RegisterType typeRevision = { @@ -493,7 +502,8 @@ int QQmlPrivate::qmlregister(RegistrationType type, void *data) type.extensionMetaObject, nullptr, QTypeRevision(), - type.structVersion > 0 ? type.finalizerCast : -1 + type.structVersion > 0 ? type.finalizerCast : -1, + creationMethod }; QQmlPrivate::RegisterSequentialContainer sequenceRevision = { |