QML supports network transparency by using URLs (rather than file names) for all references from a QML document to other content:
Image { source: "http://www.example.com/images/logo.png" }
Since a relative URL is the same as a relative file, development of QML on regular file systems remains simple:
Image { source: "images/logo.png" }
Network transparency is supported throughout QML, for example:
source
property of
FontLoader
is a URL
url
property of
WebView
(obviously!)
Even QML types themselves can be on the network - if the
QML 查看器
is used to load
http://example.com/mystuff/Hello.qml
and that content refers to a type "World", the engine will load
http://example.com/mystuff/qmldir
and resolve the type just as it would for a local file. For example if the qmldir file contains the line "World World.qml", it will load
http://example.com/mystuff/World.qml
Any other resources that
Hello.qml
referred to, usually by a relative URL, would similarly be loaded from the network.
Whenever an object has a property of type URL ( QUrl ), assigning a string to that property will actually assign an absolute URL - by resolving the string against the URL of the document where the string is used.
For example, consider this content in
http://example.com/mystuff/test.qml
:
Image { source: "images/logo.png" }
The
Image
source property will be assigned
http://example.com/mystuff/images/logo.png
, but while the QML is being developed, in say
C:\User\Fred\Documents\MyStuff\test.qml
, it will be assigned
C:\User\Fred\Documents\MyStuff\images\logo.png
.
If the string assigned to a URL is already an absolute URL, then "resolving" does not change it and the URL is assigned directly.
Because of the declarative nature of QML and the asynchronous nature of network resources, objects which reference network resource generally change state as the network resource loads. For example, an Image with a network source will initially have a
width
and
height
of 0, a
status
of
Loading
,和
progress
of 0.0. While the content loads, the
progress
will increase until the content is fully loaded from the network, at which point the
width
and
height
become the content size, the
status
becomes
Ready
,和
progress
reaches 1.0. Applications can bind to these changing states to provide visual progress indicators where appropriate, or simply bind to the
width
and
height
as if the content was a local file, adapting as those bound values change.
Note that when objects reference local files they immediately have the
Ready
status, but applications wishing to remain network transparent should not rely on this. Future versions of QML may also use asynchronous local file I/O to improve performance.
QML types such as
XmlListModel
, and JavaScript classes like
XMLHttpRequest
are intended entirely for accessing network services, which usually respond with references to content by URLs that can then be used directly in QML. For example, using these facilities to access an on-line photography service would provide the QML application with URLs to photographs, which can be directly set on an
Image
source
特性。
见
demos/declarative/flickr
for a real demonstration of this.
All network access from QML is managed by a QNetworkAccessManager 设置在 QDeclarativeEngine which executes the QML. By default, this is an unmodified Qt QNetworkAccessManager . You may set a different manager by providing a QDeclarativeNetworkAccessManagerFactory and setting it via QDeclarativeEngine::setNetworkAccessManagerFactory ()。例如, QML 查看器 sets a QDeclarativeNetworkAccessManagerFactory which creates QNetworkAccessManager that trusts HTTP Expiry headers to avoid network cache checks, allows HTTP Pipelining, adds a persistent HTTP CookieJar, a simple disk cache, and supports proxy settings.
One of the URL schemes built into Qt is the "qrc" scheme. This allows content to be compiled into the executable using Qt 资源系统 . Using this, an executable can reference QML content that is compiled into the executable:
QDeclarativeView view;
view.setSource(QUrl("qrc:/main.qml"));
The content itself can then use relative URLs, and so be transparently unaware that the content is compiled into the executable.
The
import
statement is only network transparent if it has an "as" clause.
More specifically:
import "dir"
only works on local file systems
import libraryUri
only works on local file systems
import "dir" as D
works network transparently
import libraryUrl as U
works network transparently