From c0982b004ac1e7f6b45dccbd0bbaf9604130dd2b Mon Sep 17 00:00:00 2001 From: Luca Di Sera Date: Wed, 20 Mar 2024 15:38:38 +0100 Subject: qmltc: Avoid overwriting initial properties When `qmltc` compiles a QML type, it allows the user to set the initial values for the properties of the component at construction time. Due to the order of operations that `qmltc` uses when creating a component, the compiled type keeps track of the properties for which an initial values was set to avoid overriding the value in later phases. For example, a property that is bound by its definition in the original QML file, will have that bound applied after the initial values for the properties are set. To avoid this issue, a type compiled by `qmltc` uses its knowledge of which property was initialized to avoid setting the binding for the property in the later phases. Due to an error in the code-base for `qmltc`, certain bindings for a property are still being set independently of the property having an initial value or not. In particular, one of the branches in `QmltcCodeGenerator::generate_createBindingOnProperty` was not conditioning the generated property binding on whether an initial value was set. For example, a value list property that is bound to an empty list, would have passed by the incorrect branch, so that its value would be overwritten even when the property was provided with an initial value. To avoid the issue, the branch was modified to generate code that takes into account the initialization, or lack thereof, of the property. The test that verifies the initial values behavior was modified to have a representative case for the issue. Task-number: QTBUG-120700 Change-Id: Ic3f56a8774e26ab18d3fd234a1cd28e525672e36 Reviewed-by: Ulf Hermann --- tools/qmltc/qmltccompilerpieces.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/qmltc/qmltccompilerpieces.cpp b/tools/qmltc/qmltccompilerpieces.cpp index bdb71cff4c..cd1735bc07 100644 --- a/tools/qmltc/qmltccompilerpieces.cpp +++ b/tools/qmltc/qmltccompilerpieces.cpp @@ -242,11 +242,13 @@ void QmltcCodeGenerator::generate_createBindingOnProperty( *block += epilogue; } else { QString createBindingForNonBindable = - u"QT_PREPEND_NAMESPACE(QQmlCppBinding)::createBindingForNonBindable(" + unitVarName + u" "_s + + u"QT_PREPEND_NAMESPACE(QQmlCppBinding)::createBindingForNonBindable(" + unitVarName + u", " + scope + u", " + QString::number(functionIndex) + u", " + target + u", " + QString::number(propertyIndex) + u", " + QString::number(valueTypeIndex) + u", " + propName + u")"; // Note: in this version, the binding is set implicitly + *block << u"if (!initializedCache.contains(\"%1\"))"_s.arg(p.propertyName()); *block << createBindingForNonBindable + u";"; } } -- cgit v1.2.3