QDeclarativeComponent Class Reference

The QDeclarativeComponent class encapsulates a QML component definition.

  1. #include <QDeclarativeComponent>

Inherits: QObject.

This class was introduced in Qt 4.7.

Detailed Description

The QDeclarativeComponent class encapsulates a QML component definition.

Components are reusable, encapsulated QML elements with well-defined interfaces. They are often defined in Component Files.

A QDeclarativeComponent instance can be created from a QML file. For example, if there is a main.qml file like this:

  1. import QtQuick 1.0
  2.  
  3. Item  {
  4.     width: 200
  5.     height: 200
  6. }

The following code loads this QML file as a component, creates an instance of this component using create(), and then queries the Item's width value:

  1.                 QDeclarativeEngine *engine = new QDeclarativeEngine;
  2. QDeclarativeComponent component(engine, QUrl::fromLocalFile("main.qml"));
  3.  
  4. QObject *myObject = component.create();
  5. QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(myObject);
  6. int width = item->width();  // width = 200

Network Components

If the URL passed to QDeclarativeComponent is a network resource, or if the QML document references a network resource, the QDeclarativeComponent has to fetch the network data before it is able to create objects. In this case, the QDeclarativeComponent will have a Loading status. An application will have to wait until the component is Ready before calling QDeclarativeComponent::create().

The following example shows how to load a QML file from a network resource. After creating the QDeclarativeComponent, it tests whether the component is loading. If it is, it connects to the QDeclarativeComponent::statusChanged() signal and otherwise calls the continueLoading() method directly. Note that QDeclarativeComponent::isLoading() may be false for a network component if the component has been cached and is ready immediately.

  1. MyApplication::MyApplication()
  2.  {
  3.     // ...
  4.     component = new QDeclarativeComponent(engine, QUrl("http://www.example.com/main.qml"));
  5.     if (component->isLoading())
  6.         QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)),
  7.                          this, SLOT(continueLoading()));
  8.     else
  9.         continueLoading();
  10. }
  11.  
  12. void MyApplication::continueLoading()
  13.  {
  14.     if (component->isError())  {
  15.         qWarning() << component->errors();
  16.     } else  {
  17.         QObject *myObject = component->create();
  18.     }
  19. }

See also Using QML Bindings in C++ Applications and Integrating QML Code with Existing Qt UI Code.

Public Types

Toggle detailsenum QDeclarativeComponent::

StatusStatus { Null , Ready , Loading , Error 3 ...} { Null , Ready , Loading , Error 3 }

Specifies the loading status of the QDeclarativeComponent.

ConstantValueDescription
QDeclarativeComponent::Null 0 This QDeclarativeComponent has no data. Call loadUrl() or setData() to add QML content.
QDeclarativeComponent::Ready 1 This QDeclarativeComponent is ready and create() may be called.
QDeclarativeComponent::Loading 2 This QDeclarativeComponent is loading network data.
QDeclarativeComponent::Error 3 An error has occurred. Call errors() to retrieve a list of {QDeclarativeError}{errors}.

Look up this member in the source code.

    Properties

    Toggle detailsprogressprogress : const qreal

    The progress of loading the component, from 0.0 (nothing loaded) to 1.0 (finished).

    Access functions:

    Look up this member in the source code.

    Toggle detailsstatusstatus : const Status

    The component's current status.

    Access functions:

    Look up this member in the source code.

    Toggle detailsurlurl : const QUrl

    The component URL. This is the URL passed to either the constructor, or the loadUrl() or setData() methods.

    Access functions:

    • QUrl url () const

    Look up this member in the source code.

      Public Functions

      Toggle details QDeclarativeComponent

      QDeclarativeComponentQDeclarativeComponent ( QDeclarativeEngine *engine , QObject *parent=0 ...) ( QDeclarativeEngine *engine , QObject *parent=0 )

      Create a QDeclarativeComponent with no data and give it the specified engine and parent. Set the data with setData().

      Look up this member in the source code.

      Toggle details QDeclarativeComponent

      QDeclarativeComponentQDeclarativeComponent ( QDeclarativeEngine *engine , const QString &fileName , QObject *parent=0 ...) ( QDeclarativeEngine *engine , const QString &fileName , QObject *parent=0 )

      Create a QDeclarativeComponent from the given fileName and give it the specified parent and engine.

      See also loadUrl().

      Look up this member in the source code.

      Toggle details QDeclarativeComponent

      QDeclarativeComponentQDeclarativeComponent ( QDeclarativeEngine *engine , const QUrl &url , QObject *parent=0 ...) ( QDeclarativeEngine *engine , const QUrl &url , QObject *parent=0 )

      Create a QDeclarativeComponent from the given url and give it the specified parent and engine.

      Ensure that the URL provided is full and correct, in particular, use QUrl::fromLocalFile() when loading a file from the local filesystem.

      See also loadUrl().

      Look up this member in the source code.

      Toggle details QDeclarativeComponent

      ~QDeclarativeComponent~QDeclarativeComponent () () [virtual]

      Destruct the QDeclarativeComponent.

      Look up this member in the source code.

      Toggle details QDeclarativeComponent

      Q_REVISIONQ_REVISION () ()[protected]

      Look up this member in the source code.

      Toggle details QObject * QDeclarativeComponent

      beginCreatebeginCreate ( QDeclarativeContext *context ...) ( QDeclarativeContext *context ) [virtual]

      This method provides more advanced control over component instance creation. In general, programmers should use QDeclarativeComponent::create() to create a component.

      Create an object instance from this component. Returns 0 if creation failed. context specifies the context within which to create the object instance.

      When QDeclarativeComponent constructs an instance, it occurs in three steps:

      1. The object hierarchy is created, and constant values are assigned.
      2. Property bindings are evaluated for the the first time.
      3. If applicable, QDeclarativeParserStatus::componentComplete() is called on objects.

      QDeclarativeComponent::beginCreate() differs from QDeclarativeComponent::create() in that it only performs step 1. QDeclarativeComponent::completeCreate() must be called to complete steps 2 and 3.

      This breaking point is sometimes useful when using attached properties to communicate information to an instantiated component, as it allows their initial values to be configured before property bindings take effect.

      Look up this member in the source code.

      Toggle details void QDeclarativeComponent

      completeCreatecompleteCreate () () [virtual]

      This method provides more advanced control over component instance creation. In general, programmers should use QDeclarativeComponent::create() to create a component.

      Complete a component creation begin with QDeclarativeComponent::beginCreate().

      Look up this member in the source code.

      Toggle details QObject * QDeclarativeComponent

      createcreate ( QDeclarativeContext *context=0 ( QDeclarativeContext *context=0 ) [virtual]

      Create an object instance from this component. Returns 0 if creation failed. context specifies the context within which to create the object instance.

      If context is 0 (the default), it will create the instance in the engine' s root context.

      Look up this member in the source code.

      Toggle details QDeclarativeContext * QDeclarativeComponent

      creationContextcreationContext () ()const

      Returns the QDeclarativeContext the component was created in. This is only valid for components created directly from QML.

      Look up this member in the source code.

      Toggle details QList <QDeclarativeError > QDeclarativeComponent

      errorserrors () ()const

      Return the list of errors that occurred during the last compile or create operation. An empty list is returned if isError() is not set.

      Look up this member in the source code.

      Toggle details bool QDeclarativeComponent

      isErrorisError () ()const

      Returns true if status() == QDeclarativeComponent::Error.

      Look up this member in the source code.

      Toggle details bool QDeclarativeComponent

      isLoadingisLoading () ()const

      Returns true if status() == QDeclarativeComponent::Loading.

      Look up this member in the source code.

      Toggle details bool QDeclarativeComponent

      isNullisNull () ()const

      Returns true if status() == QDeclarativeComponent::Null.

      Look up this member in the source code.

      Toggle details bool QDeclarativeComponent

      isReadyisReady () ()const

      Returns true if status() == QDeclarativeComponent::Ready.

      Look up this member in the source code.

      Toggle details void QDeclarativeComponent

      loadUrlloadUrl ( const QUrl &url ) ( const QUrl &url )

      Load the QDeclarativeComponent from the provided url.

      Ensure that the URL provided is full and correct, in particular, use QUrl::fromLocalFile() when loading a file from the local filesystem.

      Look up this member in the source code.

      Toggle details void QDeclarativeComponent

      setDatasetData ( const QByteArray &data , const QUrl &url ...) ( const QByteArray &data , const QUrl &url )

      Sets the QDeclarativeComponent to use the given QML data. If url is provided, it is used to set the component name and to provide a base path for items resolved by this component.

      Look up this member in the source code.

        Signals

        Toggle details void QDeclarativeComponent

        progressChangedprogressChanged ( qreal progress ) ( qreal progress ) [signal]

        Emitted whenever the component's loading progress changes. progress will be the current progress between 0.0 (nothing loaded) and 1.0 (finished).

        Look up this member in the source code.

        Toggle details void QDeclarativeComponent

        statusChangedstatusChanged ( QDeclarativeComponent::Status status ...) ( QDeclarativeComponent::Status status ) [signal]

        Emitted whenever the component's status changes. status will be the new status.

        Look up this member in the source code.

          Notes provided by the Qt Community

          No notes