Qt Reference Documentation

[Previous: Dynamic Object Management] [Next: Internationalization]

# Network Transparency

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:

- Fonts - the `source` property of FontLoader is a URL
- WebViews - the `url` property of WebView (obviously!)

Even QML types themselves can be on the network - if the QML Viewer 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.

## Relative vs. Absolute URLs

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.

## Progressive Loading

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`, and a `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`, and the `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.

## Accessing Network Services

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` property.

See the `demos/declarative/flickr` for a real demonstration of this.

## Configuring the Network Access Manager

All network access from QML is managed by a QNetworkAccessManager set on the 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(). For example, the QML Viewer 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.

## QRC Resources

One of the URL schemes built into Qt is the "qrc" scheme. This allows content to be compiled into the executable using The Qt Resource System. Using this, an executable can reference QML content that is compiled into the executable:

         QDeclarativeView *canvas = new QDeclarativeView;
         canvas->setUrl(QUrl("qrc:/dial.qml"));

The content itself can then use relative URLs, and so be transparently unaware that the content is compiled into the executable.

## Limitations

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