url
type refers to a resource locator (like a file name, for example). It can be either absolute, e.g. "http://qt-project.org", or relative, e.g. "pics/logo.png". A relative URL is resolved relative to the URL of the containing component.
For example, the following assigns a valid URL to the
Image::source
property, which is of type
url
:
Image { source: "pics/logo.png" }
When integrating with C++, note that any
QUrl
值
passed into QML from C++
is automatically converted into a
url
value, and vice-versa.
Alternatively you may convert your
url
到
URL
object in order to access and modify its components:
var urlObject = new URL(url);
注意:
In Qt 5, URLs were automatically resolved based on the current context when assigning them to any
url
property. This made it impossible to work with relative URLs and it created inconsistent behavior when reading back a URL previously written to a property. Therefore, the behavior was changed in Qt 6.
注意: When referring to files stored with the Qt 资源系统 from within QML, you should use "qrc:///" instead of ":/" as QML requires URL paths. Relative URLs resolved from within that file will use the same protocol.
Additionally, URLs may contain encoded characters using the 'percent-encoding' scheme specified by
RFC 3986
. These characters will be preserved within properties of type
url
, to allow QML code to construct precise URL values.
For example, a local file containing a '#' character, which would normally be interpreted as the beginning of the URL 'fragment' element, can be accessed by encoding the characters of the file name:
Image { source: encodeURIComponent("/tmp/test#1.png") }
此基本类型由 QML 语言提供。
另请参阅 QML 基本类型 .