Tooling: Also clear singletons when resetting preview

If we don't drop the singletons, any changes to them won't be picked up.

Fixes: QTBUG-140914
Change-Id: I66e240574f8ca3a44a1f39177fc7fe4de8f9b468
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
(cherry picked from commit 65dd594833)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ulf Hermann 2025-10-08 12:51:11 +02:00 committed by Qt Cherry-pick Bot
parent f4909c51c0
commit e8c9ae337d
5 changed files with 41 additions and 0 deletions

View File

@ -119,6 +119,7 @@ void QQmlPreviewHandler::loadUrl(const QUrl &url)
m_lastPosition.loadWindowPositionSettings(url);
QQmlEngine *engine = m_engines.front();
engine->clearSingletons();
engine->clearComponentCache();
m_component.reset(new QQmlComponent(engine, url, this));

View File

@ -0,0 +1,6 @@
pragma Singleton
import QtQml
QtObject {
property int col: 0
}

View File

@ -0,0 +1,2 @@
module M
singleton S 1.0 S.qml

View File

@ -0,0 +1,7 @@
import QtQuick
import M
Window {
objectName: S.col
Component.onCompleted: console.log("col", objectName)
}

View File

@ -54,6 +54,7 @@ private slots:
void unhandledFiles();
void updateFile();
void qqcStyleSelection();
void singleton();
};
tst_QQmlPreview::tst_QQmlPreview()
@ -443,6 +444,30 @@ void tst_QQmlPreview::qqcStyleSelection()
QVERIFY(m_serviceErrors.isEmpty());
}
void tst_QQmlPreview::singleton()
{
const QString file("singletonUser.qml");
QCOMPARE(startQmlProcess(file, {"QML_IMPORT_PATH=" + dataDirectory()}), ConnectSuccess);
QVERIFY(m_client);
QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
m_client->triggerLoad(testFileUrl(file));
QTRY_VERIFY(m_files.contains(testFile(file)));
verifyProcessOutputContains("col 0");
QFile input(testFile("M/S.qml"));
QVERIFY(input.open(QIODevice::ReadOnly));
QByteArray contents = input.readAll();
contents.replace("0", "5");
serveFile(testFile("M/S.qml"), contents);
m_client->triggerLoad(testFileUrl(file));
verifyProcessOutputContains("col 5");
m_process->stop();
QTRY_COMPARE(m_client->state(), QQmlDebugClient::NotConnected);
QVERIFY(m_serviceErrors.isEmpty());
}
QTEST_MAIN(tst_QQmlPreview)
#include "tst_qqmlpreview.moc"