aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlbuiltinfunctions.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-09-09 12:44:28 +0200
committerUlf Hermann <ulf.hermann@qt.io>2021-09-13 14:42:09 +0200
commit68cd898b8eae183f4b3d64f056c33f33b393907e (patch)
tree5fdaabf6fc6b4fb2e6687ae15b1da30fe800d912 /src/qml/qml/qqmlbuiltinfunctions.cpp
parent9025ea98cad57696aeee41a614cf21e9938aa527 (diff)
Provide more options for handling URLs in the "Qt" object
You may want to force a coercion to the url type without resolving the URL or you may want to resolve the URL in a different context than the caller's. Pick-to: 6.2 Task-number: QTBUG-95587 Change-Id: I367259f3636675f9aa873f9a45d2037c130261ea Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlbuiltinfunctions.cpp')
-rw-r--r--src/qml/qml/qqmlbuiltinfunctions.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/qml/qml/qqmlbuiltinfunctions.cpp b/src/qml/qml/qqmlbuiltinfunctions.cpp
index 388b6153fd..fe8420e27f 100644
--- a/src/qml/qml/qqmlbuiltinfunctions.cpp
+++ b/src/qml/qml/qqmlbuiltinfunctions.cpp
@@ -814,9 +814,30 @@ bool QtObject::openUrlExternally(const QUrl &url) const
}
/*!
+ \qmlmethod url Qt::url(url url)
+
+ Returns \a url verbatim. This can be used to force a type coercion to \c url.
+ In contrast to Qt.resolvedUrl() this retains any relative URLs. As strings
+ are implicitly converted to urls, the function can be called with a string
+ as argument, and will then return a url.
+
+ \sa resolvedUrl()
+*/
+QUrl QtObject::url(const QUrl &url) const
+{
+ return url;
+}
+
+/*!
\qmlmethod url Qt::resolvedUrl(url url)
Returns \a url resolved relative to the URL of the caller.
+
+ If there is no caller or the caller is not associated with a QML context,
+ returns \a url resolved relative to the QML engine's base URL. If the QML
+ engine has no base URL, just returns \a url.
+
+ \sa url()
*/
QUrl QtObject::resolvedUrl(const QUrl &url) const
{
@@ -828,6 +849,29 @@ QUrl QtObject::resolvedUrl(const QUrl &url) const
}
/*!
+ \qmlmethod url Qt::resolvedUrl(url url, object context)
+
+ Returns \a url resolved relative to the URL of the QML context of
+ \a context. If \a context is not associated with a QML context,
+ returns \a url resolved relative to the QML engine's base URL. If
+ the QML engine has no base URL, just returns \a url.
+
+ \sa url()
+*/
+QUrl QtObject::resolvedUrl(const QUrl &url, QObject *context) const
+{
+ if (context) {
+ QQmlData *data = QQmlData::get(context);
+ if (data && data->outerContext)
+ return data->outerContext->resolvedUrl(url);
+ }
+
+ if (QQmlEngine *engine = qmlEngine())
+ return engine->baseUrl().resolved(url);
+ return url;
+}
+
+/*!
\qmlmethod list<string> Qt::fontFamilies()
Returns a list of the font families available to the application.