aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlapplicationengine.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-02-07 16:59:51 +0100
committerUlf Hermann <ulf.hermann@qt.io>2020-03-19 15:04:55 +0100
commit75563fa761b94e049de0a29e91591610b0d15745 (patch)
treef013158c9d571a7c109572725de7cbfd1e73f51f /src/qml/qml/qqmlapplicationengine.cpp
parent585ee10c63d9e69dce07674db99c92a3814da157 (diff)
Clean up QQmlFileSelector
The global static map of file selectors is not thread safe. If you add a file selector to engine A running in thread 1, you cannot at the same time retrieve the file selector of engine B running in thread 2. Alas, we only need the static map to discern file selectors from other URL interceptors, and we only need to do this on QQmlFileSelector::get(), which we don't use. Unfortunately it is public API, though. Deprecate QQmlFileSelector::get() and add a different hack to discern the interceptors for the case that it's still called. Also remove the duplicate setExtraSelectors() method. For this to work, we also add a method to QQmlApplicationEngine that allows us to pass extra file selectors and delay the initialization of QQmlApplicationEngine's QQmlFileSelector until the first file is loaded. [ChangeLog][QML] QQmlFileSelector::get() is deprecated. You can use the new method QQmlAplicationEngine::setExtraFileSelectors() to pass extra selectors to QQmlApplicationEngine's internal QQmlFileSelector. Manually created QQmlFileSelectors should be configured immediately after creation, before they are used by the QQmlEngine. Change-Id: Ia61a93777dc910b441a03ffb42d35a2a224c0e26 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlapplicationengine.cpp')
-rw-r--r--src/qml/qml/qqmlapplicationengine.cpp30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/qml/qml/qqmlapplicationengine.cpp b/src/qml/qml/qqmlapplicationengine.cpp
index 7d961cd0c7..a633d3f58b 100644
--- a/src/qml/qml/qqmlapplicationengine.cpp
+++ b/src/qml/qml/qqmlapplicationengine.cpp
@@ -81,7 +81,8 @@ void QQmlApplicationEnginePrivate::init()
else
delete qtTranslator;
#endif
- new QQmlFileSelector(q,q);
+ auto *selector = new QQmlFileSelector(q,q);
+ selector->setExtraSelectors(extraFileSelectors);
QCoreApplication::instance()->setProperty("__qml_using_qqmlapplicationengine", QVariant(true));
}
@@ -115,6 +116,11 @@ void QQmlApplicationEnginePrivate::startLoad(const QUrl &url, const QByteArray &
{
Q_Q(QQmlApplicationEngine);
+ if (!isInitialized) {
+ init();
+ isInitialized = true;
+ }
+
if (url.scheme() == QLatin1String("file") || url.scheme() == QLatin1String("qrc")) {
QFileInfo fi(QQmlFile::urlToLocalFileOrQrc(url));
translationsDirectory = fi.path() + QLatin1String("/i18n");
@@ -227,8 +233,6 @@ void QQmlApplicationEnginePrivate::finishLoad(QQmlComponent *c)
QQmlApplicationEngine::QQmlApplicationEngine(QObject *parent)
: QQmlEngine(*(new QQmlApplicationEnginePrivate(this)), parent)
{
- Q_D(QQmlApplicationEngine);
- d->init();
QJSEnginePrivate::addToDebugServer(this);
}
@@ -312,6 +316,26 @@ void QQmlApplicationEngine::setInitialProperties(const QVariantMap &initialPrope
}
/*!
+ Sets the \a extraFileSelectors to be passed to the internal QQmlFileSelector
+ used for resolving URLs to local files. The \a extraFileSelectors are applied
+ when the first QML file is loaded. Setting them afterwards has no effect.
+
+ \sa QQmlFileSelector
+ \sa QFileSelector::setExtraSelectors
+ \since 6.0
+*/
+void QQmlApplicationEngine::setExtraFileSelectors(const QStringList &extraFileSelectors)
+{
+ Q_D(QQmlApplicationEngine);
+ if (d->isInitialized) {
+ qWarning() << "QQmlApplicationEngine::setExtraFileSelectors()"
+ << "called after loading QML files. This has no effect.";
+ } else {
+ d->extraFileSelectors = extraFileSelectors;
+ }
+}
+
+/*!
Loads the QML given in \a data. The object tree defined by \a data is
instantiated immediately.