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  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QDeclarativeComponent Class Reference

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

 #include <QDeclarativeComponent>

Inherits: QObject.

This class was introduced in Qt 4.7.

Public Types

enum Status { Null, Ready, Loading, Error }

Properties

  • 1 property inherited from QObject

Public Functions

QDeclarativeComponent ( QDeclarativeEngine * engine, QObject * parent = 0 )
QDeclarativeComponent ( QDeclarativeEngine * engine, const QString & fileName, QObject * parent = 0 )
QDeclarativeComponent ( QDeclarativeEngine * engine, const QUrl & url, QObject * parent = 0 )
virtual ~QDeclarativeComponent ()
virtual QObject * beginCreate ( QDeclarativeContext * context )
virtual void completeCreate ()
virtual QObject * create ( QDeclarativeContext * context = 0 )
QDeclarativeContext * creationContext () const
QList<QDeclarativeError> errors () const
bool isError () const
bool isLoading () const
bool isNull () const
bool isReady () const
void loadUrl ( const QUrl & url )
qreal progress () const
void setData ( const QByteArray & data, const QUrl & url )
Status status () const
QUrl url () const
  • 29 public functions inherited from QObject

Signals

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

Protected Functions

Q_REVISION ()
  • 7 protected functions inherited from QObject

Additional Inherited Members

  • 1 public slot inherited from QObject
  • 5 static public members inherited from QObject

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:

 import QtQuick 1.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:

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

 QObject *myObject = component.create();
 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(myObject);
 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.

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

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

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

Member Type Documentation

enum QDeclarativeComponent::Status

Specifies the loading status of the QDeclarativeComponent.

ConstantValueDescription
QDeclarativeComponent::Null0This QDeclarativeComponent has no data. Call loadUrl() or setData() to add QML content.
QDeclarativeComponent::Ready1This QDeclarativeComponent is ready and create() may be called.
QDeclarativeComponent::Loading2This QDeclarativeComponent is loading network data.
QDeclarativeComponent::Error3An error has occurred. Call errors() to retrieve a list of {QDeclarativeError}{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 ( QDeclarativeComponent::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

QDeclarativeComponent::QDeclarativeComponent ( QDeclarativeEngine * engine, QObject * parent = 0 )

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

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

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

QDeclarativeComponent::~QDeclarativeComponent () [virtual]

Destruct the QDeclarativeComponent.

QDeclarativeComponent::Q_REVISION () [protected]

QObject * QDeclarativeComponent::beginCreate ( 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.

void QDeclarativeComponent::completeCreate () [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().

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

QDeclarativeContext * QDeclarativeComponent::creationContext () const

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

QList<QDeclarativeError> QDeclarativeComponent::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.

bool QDeclarativeComponent::isError () const

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

bool QDeclarativeComponent::isLoading () const

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

bool QDeclarativeComponent::isNull () const

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

bool QDeclarativeComponent::isReady () const

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

void QDeclarativeComponent::loadUrl ( 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.

void QDeclarativeComponent::progressChanged ( 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).

void QDeclarativeComponent::setData ( 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.

void QDeclarativeComponent::statusChanged ( QDeclarativeComponent::Status status ) [signal]

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

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 77
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 6
Page suivante

Le blog Digia au hasard

Logo

Créer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut Cuvelier

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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 4.7-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