diff options
author | Luca Di Sera <luca.disera@qt.io> | 2024-03-20 15:49:34 +0100 |
---|---|---|
committer | Luca Di Sera <luca.disera@qt.io> | 2024-03-21 21:00:14 +0100 |
commit | d1241024e6fe20445a1cd843969ec9f518357fb8 (patch) | |
tree | 35471c9502387ffdd9333279bab1303a264a2f47 /tools | |
parent | c0982b004ac1e7f6b45dccbd0bbaf9604130dd2b (diff) |
qmltc: Support setting initial values for reference list properties
When `qmltc` compiles a QML type to C++, it generates a proxy object for
the compiled type, `PropertyInitializer`, that knowns how to set the
top-level property of the component.
A user can then pass a call-back to the constructor for the compiled
type and use an instance of the generated `PropertyInitializer` to set
the initial values for the top-level properties of the component.
Currently, the generated `PropertyInitializer` would refuse to generate
a setter for properties whose type is a list of object types, impeding
the user from being able to set the initial values for properties of
this kind.
To allow setting the initial values for properties whose type is a list
of object types, `qmltc` will generate a setter for those properties
when generating the `PropertyInitializer` class for a type.
The free function `compilePropertyInitializer` in "qmltccompiler.cpp",
that is responsible for generating the `PropertyInitializer` class for a
type, was modified to produce a setter for properties whose type is a
list of object types.
The test that verifies the initial values behavior was modified to have
representative cases for the modified feature.
Fixes: QTBUG-120700
Change-Id: I4a023263ca3dfa3c77c11d1522b8be47b7958109
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qmltc/qmltccompiler.cpp | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/tools/qmltc/qmltccompiler.cpp b/tools/qmltc/qmltccompiler.cpp index 88c3d230e2..f632748241 100644 --- a/tools/qmltc/qmltccompiler.cpp +++ b/tools/qmltc/qmltccompiler.cpp @@ -416,7 +416,7 @@ static void compilePropertyInitializer(QmltcType ¤t, const QQmlJSScope::Co for (auto& property: properties) { if (property.index() == -1) continue; if (property.isPrivate()) continue; - if (!property.isWritable() || qIsReferenceTypeList(property)) continue; + if (!property.isWritable() && !qIsReferenceTypeList(property)) continue; const QString name = property.propertyName(); @@ -426,11 +426,28 @@ static void compilePropertyInitializer(QmltcType ¤t, const QQmlJSScope::Co compiledSetter.userVisible = true; compiledSetter.returnType = u"void"_s; compiledSetter.name = QmltcPropertyData(property).write; - compiledSetter.parameterList.emplaceBack( - QQmlJSUtils::constRefify(getUnderlyingType(property)), name + u"_", QString() - ); - if (QQmlJSUtils::bindablePropertyHasDefaultAccessor(property, QQmlJSUtils::PropertyAccessor_Write)) { + if (qIsReferenceTypeList(property)) { + compiledSetter.parameterList.emplaceBack( + QQmlJSUtils::constRefify(u"QList<%1*>"_s.arg(property.type()->valueType()->internalName())), + name + u"_", QString() + ); + } else { + compiledSetter.parameterList.emplaceBack( + QQmlJSUtils::constRefify(getUnderlyingType(property)), name + u"_", QString() + ); + } + + if (qIsReferenceTypeList(property)) { + compiledSetter.body << u"QQmlListReference list_ref_(&%1, \"%2\");"_s.arg( + current.propertyInitializer.component.name, name + ); + compiledSetter.body << u"list_ref_.clear();"_s; + compiledSetter.body << u"for (const auto& list_item_ : %1_)"_s.arg(name); + compiledSetter.body << u" list_ref_.append(list_item_);"_s; + } else if ( + QQmlJSUtils::bindablePropertyHasDefaultAccessor(property, QQmlJSUtils::PropertyAccessor_Write) + ) { compiledSetter.body << u"%1.%2().setValue(%3_);"_s.arg( current.propertyInitializer.component.name, property.bindable(), name); } else if (property.write().isEmpty() || isFromExtension(property, type)) { |