diff options
author | Jaeyoon Jung <jaeyoon.jung@lge.com> | 2018-02-23 08:56:31 +0900 |
---|---|---|
committer | Dominik Holland <dominik.holland@qt.io> | 2020-06-10 12:51:53 +0200 |
commit | bba1b19a18a04060942e5580e36daccf9c830b61 (patch) | |
tree | 91e53bb6d343a19e890efca9758d0da53f885f57 | |
parent | 334f8674b25c8086b2cd2660cc159030867c0a00 (diff) |
Fix baseUrl returning an empty url
Do not append the directory separator if the current path is the root
directory. Otherwise it is treated as a remote path incorrectly and an
empty url is returned as a result.
Change-Id: Icc62114702265e9e1b247624370a78999e564d5f
Task-number: QTBUG-83121
Pick-to: 5.15
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r-- | src/qml/qml/qqmlengine.cpp | 4 | ||||
-rw-r--r-- | tests/auto/qml/qqmlengine/tst_qqmlengine.cpp | 13 |
2 files changed, 16 insertions, 1 deletions
diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index 381d934b8b..c63418cb0b 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -1285,7 +1285,9 @@ QUrl QQmlEngine::baseUrl() const { Q_D(const QQmlEngine); if (d->baseUrl.isEmpty()) { - return QUrl::fromLocalFile(QDir::currentPath() + QDir::separator()); + const QString currentPath = QDir::currentPath(); + const QString rootPath = QDir::rootPath(); + return QUrl::fromLocalFile((currentPath == rootPath) ? rootPath : (currentPath + QDir::separator())); } else { return d->baseUrl; } diff --git a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp index a0cc35d90b..39e986f12a 100644 --- a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp +++ b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp @@ -214,6 +214,19 @@ void tst_qqmlengine::baseUrl() engine.setBaseUrl(cwd); QCOMPARE(engine.baseUrl(), cwd); QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd.resolved(QUrl("main.qml"))); + + + const QString testPath = QDir::currentPath() + QLatin1String("/"); + const QString rootPath = QDir::rootPath(); + engine.setBaseUrl(QUrl()); + + // Check that baseUrl returns a url to a localFile + QCOMPARE(engine.baseUrl().toLocalFile(), testPath); + + QDir::setCurrent(QDir::rootPath()); + + // Make sure this also works when in the rootPath + QCOMPARE(engine.baseUrl().toLocalFile(), rootPath); } void tst_qqmlengine::contextForObject() |