本页内容

url QML Value Type

资源定位器。更多...

详细说明

url 类型指代一个资源定位符(例如文件名)。它可以是绝对路径,例如“http://qt-project.org”,也可以是相对路径,例如“pics/logo.png”。 相对 URL 是相对于包含该组件的组件的 URL 进行解析的。

例如,以下代码将一个有效的URL赋值给类型为url 的Image::source 属性:

Image { source: "pics/logo.png" }

在与 C++ 集成时,请注意:从 C++ 传递到 QML的任何 `QUrl ` 值都会自动转换为 `url ` 值,反之亦然。

或者,您可以将 `url ` 转换为 `URL` 对象,以便访问和修改其组件:

var urlObject = new URL(url);

注意:在 Qt 5 中,将 URL 赋值给任何url 属性时,系统会根据当前上下文自动解析 URL。这导致无法处理相对 URL,并且在读取先前写入属性的 URL 时会产生不一致的行为。因此,Qt 6 中更改了该行为:URL 在赋值时不再自动解析。 使用 URL 的各个元素必须自行解析 URL。

注意:在 Qml 中引用通过Qt 资源系统存储的文件时 ,应使用“qrc:///”而非“:/”,因为 Qml 要求使用 URL 路径。从该文件内部解析的相对 URL 将使用相同的协议。

此外,URL 可能包含采用RFC 3986 规定的“百分比编码”方案进行编码的字符。这些字符将在类型为url 的属性中被保留,以便 QML 代码能够构建精确的 URL 值。

例如,一个包含“#”字符的本地文件(该字符通常会被解释为 URL“片段”元素的开头),可以通过对文件名中的字符进行编码来访问:

Image { source: encodeURIComponent("/tmp/test#1.png") }

此值类型由 QML 语言提供。

另请参阅 QML 值类型。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.