Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QQmlComponent Class

The QQmlComponent class encapsulates a QML component definition More...

 #include <QQmlComponent>

Inherits: QObject.

QQmlComponent is instantiated by QML element Component

This class was introduced in Qt 5.0.

Public Types

enum CompilationMode { PreferSynchronous, Asynchronous }
enum Status { Null, Ready, Loading, Error }

Properties

  • 1 property inherited from QObject

Public Functions

QQmlComponent(QQmlEngine * engine, QObject * parent = 0)
QQmlComponent(QQmlEngine * engine, const QString & fileName, QObject * parent = 0)
QQmlComponent(QQmlEngine * engine, const QString & fileName, CompilationMode mode, QObject * parent = 0)
QQmlComponent(QQmlEngine * engine, const QUrl & url, QObject * parent = 0)
QQmlComponent(QQmlEngine * engine, const QUrl & url, CompilationMode mode, QObject * parent = 0)
virtual ~QQmlComponent()
virtual QObject * beginCreate(QQmlContext * publicContext)
virtual void completeCreate()
virtual QObject * create(QQmlContext * context = 0)
void create(QQmlIncubator &, QQmlContext * context = 0, QQmlContext * forContext = 0)
QQmlContext * creationContext() const
QList<QQmlError> errors() const
bool isError() const
bool isLoading() const
bool isNull() const
bool isReady() const
qreal progress() const
Status status() const
QUrl url() const
  • 31 public functions inherited from QObject

Public Slots

void loadUrl(const QUrl & url)
void loadUrl(const QUrl & url, CompilationMode mode)
void setData(const QByteArray & data, const QUrl & url)
  • 1 public slot inherited from QObject

Signals

void progressChanged(qreal progress)
void statusChanged(QQmlComponent::Status status)

Protected Functions

void createObject(QQmlV8Function *)
void incubateObject(QQmlV8Function *)
  • 9 protected functions inherited from QObject

Additional Inherited Members

  • 11 static public members inherited from QObject

Detailed Description

The QQmlComponent class encapsulates a QML component definition

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

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

 import QtQuick 2.0

 Item {
     width: 200
     height: 200
 }

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:

 QQmlEngine *engine = new QQmlEngine;
 QQmlComponent component(engine, QUrl::fromLocalFile("main.qml"));

 QObject *myObject = component.create();
 QQuickItem *item = qobject_cast<QQuickItem*>(myObject);
 int width = item->width();  // width = 200

Network Components

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

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

 MyApplication::MyApplication()
 {
     // ...
     component = new QQmlComponent(engine, QUrl("http://www.example.com/main.qml"));
     if (component->isLoading())
         QObject::connect(component, SIGNAL(statusChanged(QQmlComponent::Status)),
                          this, SLOT(continueLoading()));
     else
         continueLoading();
 }

 void MyApplication::continueLoading()
 {
     if (component->isError()) {
         qWarning() << component->errors();
     } else {
         QObject *myObject = component->create();
     }
 }

Note that the QtQuick 1 version is named QDeclarativeComponent.

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

Member Type Documentation

enum QQmlComponent::CompilationMode

Specifies whether the QQmlComponent should load the component immediately, or asynchonously.

ConstantValueDescription
QQmlComponent::PreferSynchronous0Prefer loading/compiling the component immediately, blocking the thread. This is not always possible, e.g. remote URLs will always load asynchronously.
QQmlComponent::Asynchronous1Load/compile the component in a background thread.

enum QQmlComponent::Status

Specifies the loading status of the QQmlComponent.

ConstantValueDescription
QQmlComponent::Null0This QQmlComponent has no data. Call loadUrl() or setData() to add QML content.
QQmlComponent::Ready1This QQmlComponent is ready and create() may be called.
QQmlComponent::Loading2This QQmlComponent is loading network data.
QQmlComponent::Error3An error has occurred. Call errors() to retrieve a list of {QQmlError}{errors}.

Property Documentation

progress : const qreal

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

Access functions:

qreal progress() const

Notifier signal:

void progressChanged(qreal progress)

status : const Status

The component's current status.

Access functions:

Status status() const

Notifier signal:

void statusChanged(QQmlComponent::Status status)

url : 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

Member Function Documentation

QQmlComponent::QQmlComponent(QQmlEngine * engine, QObject * parent = 0)

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

QQmlComponent::QQmlComponent(QQmlEngine * engine, const QString & fileName, QObject * parent = 0)

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

See also loadUrl().

QQmlComponent::QQmlComponent(QQmlEngine * engine, const QString & fileName, CompilationMode mode, QObject * parent = 0)

Create a QQmlComponent from the given fileName and give it the specified parent and engine. If mode is Asynchronous, the component will be loaded and compiled asynchronously.

See also loadUrl().

QQmlComponent::QQmlComponent(QQmlEngine * engine, const QUrl & url, QObject * parent = 0)

Create a QQmlComponent 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().

QQmlComponent::QQmlComponent(QQmlEngine * engine, const QUrl & url, CompilationMode mode, QObject * parent = 0)

Create a QQmlComponent from the given url and give it the specified parent and engine. If mode is Asynchronous, the component will be loaded and compiled asynchronously.

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().

QQmlComponent::~QQmlComponent() [virtual]

Destruct the QQmlComponent.

QObject * QQmlComponent::beginCreate(QQmlContext * publicContext) [virtual]

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

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

When QQmlComponent 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, QQmlParserStatus::componentComplete() is called on objects.

QQmlComponent::beginCreate() differs from QQmlComponent::create() in that it only performs step 1. QQmlComponent::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.

void QQmlComponent::completeCreate() [virtual]

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

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

QObject * QQmlComponent::create(QQmlContext * 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.

void QQmlComponent::create(QQmlIncubator &, QQmlContext * context = 0, QQmlContext * forContext = 0)

void QQmlComponent::createObject(QQmlV8Function *) [protected]

QQmlContext * QQmlComponent::creationContext() const

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

QList<QQmlError> QQmlComponent::errors() 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.

void QQmlComponent::incubateObject(QQmlV8Function *) [protected]

bool QQmlComponent::isError() const

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

bool QQmlComponent::isLoading() const

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

bool QQmlComponent::isNull() const

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

bool QQmlComponent::isReady() const

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

void QQmlComponent::loadUrl(const QUrl & url) [slot]

Load the QQmlComponent 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.

void QQmlComponent::loadUrl(const QUrl & url, CompilationMode mode) [slot]

Load the QQmlComponent from the provided url. If mode is Asynchronous, the component will be loaded and compiled asynchronously.

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

void QQmlComponent::setData(const QByteArray & data, const QUrl & url) [slot]

Sets the QQmlComponent 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.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web