diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-10-20 09:05:40 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-10-20 09:05:40 (GMT) |
commit | 7bd7f5ec6eb520b7d9940a24817b17132d9ed6a2 (patch) | |
tree | fc5c15c88eecea465aefd0f4108b289a5c9ada1e /doc | |
parent | 626d9966e2675127fb28072c7f775edb1ae40721 (diff) | |
download | Qt-7bd7f5ec6eb520b7d9940a24817b17132d9ed6a2.zip Qt-7bd7f5ec6eb520b7d9940a24817b17132d9ed6a2.tar.gz Qt-7bd7f5ec6eb520b7d9940a24817b17132d9ed6a2.tar.bz2 |
Doc
Diffstat (limited to 'doc')
-rw-r--r-- | doc/src/declarative/qtbinding.qdoc | 79 |
1 files changed, 78 insertions, 1 deletions
diff --git a/doc/src/declarative/qtbinding.qdoc b/doc/src/declarative/qtbinding.qdoc index f6fe069..1831cf8 100644 --- a/doc/src/declarative/qtbinding.qdoc +++ b/doc/src/declarative/qtbinding.qdoc @@ -44,15 +44,93 @@ \target qtbinding \title Using QML in C++ Applications +The QML API is split into three main classes - QmlEngine, QmlComponent and QmlContext. +QmlEngine provides the environment in which QML is run, QmlComponent encapsulates +\l {QML Documents}, and QmlContext allows applications to expose data to QML component instances. + +\section1 Basic Usage + +Every application requires at least one QmlEngine. A QmlEngine allows the configuration of +global settings that apply to all the QML component instances - such as the QNetworkAccessManager +that is used for network communications, and the path used for persistent storage. +Multiple QmlEngine's are only needed if the application requires these settings to differ +between QML component instances. + +\l {QML Documents} are loaded using the QmlComponent class. Each QmlComponent instance +represents a single QML document. A QmlComponent can be passed a document URL, or raw text +representing the content of the document. The document URL can be a local filesystem URL, or +any network URL supported by QNetworkAccessManager. + +QML component instances can then be created by calling the QmlComponent::create() method. Here's +an example of loading a QML document, and creating an object from it. + +\code +QmlEngine *engine = new QmlEngine(parent); +QmlComponent component(engine, QUrl("main.qml")); +QObject *myObject = component.create(); +\endcode + +\section1 Exposing Data + +QML components are instantiated in a QmlContext. A context allows the application to expose data +to the QML component instance. A single QmlContext can be used to instantiate all the objects +used by an application, or several QmlContext can be created for more fine grained control over +the data exposed to each instance. If a context is not passed to the QmlComponent::create() +method, the QmlEngine's \l {QmlEngine::rootContext()}{root context} is used. + +To expose data to a QML component instance, applications set \l {QmlContext::setContextProperty()}{context properties} which are then accessible by name from QML \l {Property Binding}s and +\l {ECMAScript Blocks}. + +\section1 Network Components + +If the URL passed to QmlComponent is a network resource, or if the QML document references a +network resource, the QmlComponent has to fetch the network data before it is able to create +objects. In this case, the QmlComponent will have a \l {QmlComponent::Loading}{Loading} +\l {QmlComponent::status()}{status}. An application will have to wait until the component +is \l {QmlComponent::Ready}{Ready} before calling \l {QmlComponent::create()}. + +The following example shows how to load a QML file from a network resource. After creating +the QmlComponent, it tests whether the component is loading. If it is, it connects to the +QmlComponent::statusChanged() signal and otherwise calls the \c {continueLoading()} method +directly. This test is necessary, even for URLs that are known to be remote, just in case +the component has been cached and is ready immediately. + +\code +MyApplication::MyApplication() +{ + // ... + component = new QmlComponent(engine, QUrl("http://www.example.com/main.qml")); + if (component->isLoading()) + QObject::connect(component, SIGNAL(statusChanged(QmlComponent::Status)), + this, SLOT(continueLoading())); + else + continueLoading(); +} + +void MyApplication::continueLoading() +{ + if (component->isError()) { + qWarning() << component->errors(); + } else { + QObject *myObject = component->create(); + } +} +\endcode + \section1 TODO \list \o QmlEngine and QmlComponent \o QmlContext and data \o QBindableMap \o QAbstractItemModel Data models +\o QmlView \endlist +*/ + +/* \section1 Overview + The QML mechanisms of data binding can also be used to bind Qt C++ objects. The data binding framework is based on Qt's property system (see the Qt documentation for more details on this system). If a binding is meant to be dynamic (where changes in one object are reflected in another object), \c NOTIFY must be specified for the property being tracked. If \c NOTIFY is not specified, any binding to that property will be an 'intialization' binding (the tracking object will be updated only once with the initial value of the tracked object). @@ -60,7 +138,6 @@ The data binding framework is based on Qt's property system (see the Qt document Relevant items can also be bound to the contents of a Qt model. For example, ListView can make use of data from a QAbstractItemModel-derived model. - \section1 Passing Data Between C++ and QML Data binding provides one method of data transfer between C++ and QML. |