From 492dc98a288763d3d026e9557ffadc018cede350 Mon Sep 17 00:00:00 2001 From: Santhosh Kumar Date: Mon, 6 Mar 2023 17:46:38 +0100 Subject: [PATCH] Fix multiple object creation in Loader QML Loader creates multiple object of same component during initial loading, if sourceComponent is assigned within state configuration and it evaluates to be true. This happens due to the effect of evaluating stage change (here in this case, setting loader source component) during component completion which internally creates component object and makes loader status be ready, even before Loader instantiates object. This patch adds a guard to check loader status and avoids object creation if status is already ready. Fixes: QTBUG-111559 Pick-to: 6.2 6.4 6.5 6.5.0 Change-Id: I6c3456cd3bc35a717c139fbd3670c305304f480a Reviewed-by: Fabian Kosmale Reviewed-by: Shawn Rutledge --- src/quick/items/qquickloader.cpp | 2 +- .../quick/qquickloader/tst_qquickloader.cpp | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/quick/items/qquickloader.cpp b/src/quick/items/qquickloader.cpp index da04a2956c..fdee5d0b35 100644 --- a/src/quick/items/qquickloader.cpp +++ b/src/quick/items/qquickloader.cpp @@ -795,7 +795,7 @@ void QQuickLoader::componentComplete() { Q_D(QQuickLoader); QQuickItem::componentComplete(); - if (active()) { + if (active() && (status() != Ready)) { if (d->loadingFromSource) d->createComponent(); d->load(); diff --git a/tests/auto/quick/qquickloader/tst_qquickloader.cpp b/tests/auto/quick/qquickloader/tst_qquickloader.cpp index af7fd237f9..3045f90169 100644 --- a/tests/auto/quick/qquickloader/tst_qquickloader.cpp +++ b/tests/auto/quick/qquickloader/tst_qquickloader.cpp @@ -108,6 +108,7 @@ private slots: void statusChangeOnlyEmittedOnce(); void setSourceAndCheckStatus(); + void loadComponentWithStates(); void asyncLoaderRace(); void noEngine(); @@ -1495,6 +1496,26 @@ void tst_QQuickLoader::setSourceAndCheckStatus() QCOMPARE(loader->status(), QQuickLoader::Null); } +void tst_QQuickLoader::loadComponentWithStates() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData(QByteArray("import QtQuick\n" + "Loader {\n" + "id: loader\n" + "property int createdObjCount: 0\n" + "states: [ State { when: true; PropertyChanges { target: loader; sourceComponent: myComp } } ]\n" + "Component { id: myComp; Item { Component.onCompleted: { ++createdObjCount } } }\n" + "}" ) + , dataDirectoryUrl()); + QScopedPointer loader(qobject_cast(component.create())); + QTest::qWait(200); + QTRY_VERIFY(loader != nullptr); + QVERIFY(loader->item()); + QCOMPARE(static_cast(loader.data())->childItems().size(), 1); + QCOMPARE(loader->property("createdObjCount").toInt(), 1); +} + void tst_QQuickLoader::asyncLoaderRace() { QQmlApplicationEngine engine;