url是资源定位。可以用来定位文件名,可以使用绝对路径,也可以使用相对路径。
绝对URL:http://qt-project.org
相对URL:pics/logo.png
例如,为Image::source听过一个有效的URL:
Image { source: "pics/logo.png" }
当一个url类型属性填写了相对URL,那么它将转换为一个URL对象,因此无法将URL的值与输入的字符串的值进行匹配。
但,可以使用Qt.resolvedUrl()
将字符串转为URL,也可使用toString()
获取URL的值。
Image {
source: "pics/logo.png"
Component.onCompleted: {
// This prints 'false'. Although "pics/logo.png" was the input string,
// it's been converted from a string to a URL, so these two are not the same.
console.log(source == "pics/logo.png")
// This prints 'true' as Qt.resovledUrl() converts the string into a
// URL with the correctly resolved path
console.log(source == Qt.resolvedUrl("pics/logo.png"))
// This prints the absolute path, e.g. "file:///path/to/pics/logo.png"
console.log(source.toString())
}
}
当使用QML引用Qt资源系统中存储的件事,应该使用"qrc:///
" 而不是":/
" 。