From c27e32def2a618cf02c6ce72941656e965b56b71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tinja=20Paavosepp=C3=A4?= Date: Wed, 9 Feb 2022 14:26:47 +0200 Subject: [PATCH] Add QML_PLUGIN_PATH environment variable to plugin path Check for contents of environment variable QML_PLUGIN_PATH and add them to Qml plugin path. In some cases it would be preferable to add additional Qml plugin paths without having to call QQmlEngine::addPluginPath() in the application code. Qt Android apps have already been doing this with QT_BUNDLED_LIBS_PATH, but the naming could be a bit misleading outside bundled Android apps. The option to provide these additional paths with environment variables is already commonly used, e.g. for other Qt plugins and Qml imports. Change-Id: Id81bb4fc6caa3c9c3adfd73be6ea45be74c5d8c4 Reviewed-by: Ulf Hermann Reviewed-by: Fabian Kosmale --- src/qml/qml/qqmlimport.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/qml/qml/qqmlimport.cpp b/src/qml/qml/qqmlimport.cpp index f51fc6dc22..bbe9185d78 100644 --- a/src/qml/qml/qqmlimport.cpp +++ b/src/qml/qml/qqmlimport.cpp @@ -1670,7 +1670,7 @@ void QQmlImports::setDesignerSupportRequired(bool b) designerSupportRequired = b; } -static QStringList parseEnvImportPath(const QString &envImportPath) +static QStringList parseEnvPath(const QString &envImportPath) { if (QDir::listSeparator() == u':') { // Double colons are interpreted as separator + resource path. @@ -1710,7 +1710,7 @@ QQmlImportDatabase::QQmlImportDatabase(QQmlEngine *e) auto addEnvImportPath = [this](const char *var) { if (Q_UNLIKELY(!qEnvironmentVariableIsEmpty(var))) { - const QStringList paths = parseEnvImportPath(qEnvironmentVariable(var)); + const QStringList paths = parseEnvPath(qEnvironmentVariable(var)); for (int ii = paths.count() - 1; ii >= 0; --ii) addImportPath(paths.at(ii)); } @@ -1722,15 +1722,19 @@ QQmlImportDatabase::QQmlImportDatabase(QQmlEngine *e) addImportPath(QStringLiteral("qrc:/qt-project.org/imports")); addImportPath(QCoreApplication::applicationDirPath()); + + auto addEnvPluginPath = [this](const char *var) { + if (Q_UNLIKELY(!qEnvironmentVariableIsEmpty(var))) { + const QStringList paths = parseEnvPath(qEnvironmentVariable(var)); + for (int ii = paths.count() - 1; ii >= 0; --ii) + addPluginPath(paths.at(ii)); + } + }; + + addEnvPluginPath("QML_PLUGIN_PATH"); #if defined(Q_OS_ANDROID) addImportPath(QStringLiteral("qrc:/android_rcc_bundle/qml")); - if (Q_UNLIKELY(!qEnvironmentVariableIsEmpty("QT_BUNDLED_LIBS_PATH"))) { - const QString envImportPath = qEnvironmentVariable("QT_BUNDLED_LIBS_PATH"); - QLatin1Char pathSep(':'); - QStringList paths = envImportPath.split(pathSep, Qt::SkipEmptyParts); - for (int ii = paths.count() - 1; ii >= 0; --ii) - addPluginPath(paths.at(ii)); - } + addEnvPluginPath("QT_BUNDLED_LIBS_PATH"); #elif defined(Q_OS_MACOS) // Add the main bundle's Resources/qml directory as an import path, so that QML modules are // found successfully when running the app from its build dir.