url QML Value Type
资源定位器。更多
详细说明
url
类型指的是资源定位器(例如文件名)。它可以是绝对 URL,如 "http://qt-project.org",也可以是相对 URL,如 "pics/logo.png"。相对 URL 是相对于包含组件的 URL 解析的。
例如,下面为Image::source 属性分配了一个有效的 URL,其类型为url
:
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。
注意: 在QtQml 中引用Qt Resource System存储的文件时,应使用 "qrc:///"而不是":/",因为QtQml 要求 URL 路径。在该文件中解析的相对 URL 将使用相同的协议。
此外,URL 可能包含使用RFC 3986 规定的 "百分比编码 "方案的编码字符。这些字符将保留在url
类型的属性中,以便 QML 代码能构建精确的 URL 值。
例如,包含 "#"字符的本地文件(通常会被解释为 URL "片段 "元素的开头),可以通过对文件名的字符编码来访问:
Image { source: encodeURIComponent("/tmp/test#1.png") }
该值类型由 QML 语言提供。
另请参阅 QML 值类型。
© 2025 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.