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  · 

Component

The Component element encapsulates a QML component definition. More...

Inherits QtObject

Component instantiates the C++ class QQmlComponent

This type was introduced in Qt 4.7.

Properties

Attached Signals

Methods

Detailed Description

Components are reusable, encapsulated QML elements with well-defined interfaces.

Components are often defined by component files - that is, .qml files. The Component element essentially allows QML components to be defined inline, within a QML document, rather than as a separate QML file. This may be useful for reusing a small component within a QML file, or for defining a component that logically belongs with other QML components within a file.

For example, here is a component that is used by multiple Loader objects. It contains a single item, a Rectangle:


Notice that while a Rectangle by itself would be automatically rendered and displayed, this is not the case for the above rectangle because it is defined inside a Component. The component encapsulates the QML elements within, as if they were defined in a separate QML file, and is not loaded until requested (in this case, by the two Loader objects).

Defining a Component is similar to defining a QML document. A QML document has a single top-level item that defines the behaviors and properties of that component, and cannot define properties or behaviors outside of that top-level item. In the same way, a Component definition contains a single top level item (which in the above example is a Rectangle) and cannot define any data outside of this item, with the exception of an id (which in the above example is redSquare).

The Component element is commonly used to provide graphical components for views. For example, the ListView::delegate property requires a Component to specify how each list item is to be displayed.

Component objects can also be created dynamically using Qt.createComponent().

Property Documentation

read-onlyprogress : real

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


read-onlystatus : enumeration

This property holds the status of component loading. It can be one of:

  • Component.Null - no data is available for the component
  • Component.Ready - the component has been loaded, and can be used to create instances.
  • Component.Loading - the component is currently being loaded
  • Component.Error - an error occurred while loading the component. Calling errorString() will provide a human-readable description of any errors.

read-onlyurl : url

The component URL. This is the URL that was used to construct the component.


Attached Signal Documentation

Component::onCompleted()

Emitted after component "startup" has completed. This can be used to execute script code at startup, once the full QML environment has been established.

The Component::onCompleted attached property can be applied to any element. The order of running the onCompleted scripts is undefined.

 Rectangle {
     Component.onCompleted: console.log("Completed Running!")
     Rectangle {
         Component.onCompleted: console.log("Nested Completed Running!")
     }
 }

Component::onDestruction()

Emitted as the component begins destruction. This can be used to undo work done in the onCompleted signal, or other imperative code in your application.

The Component::onDestruction attached property can be applied to any element. However, it applies to the destruction of the component as a whole, and not the destruction of the specific object. The order of running the onDestruction scripts is undefined.

 Rectangle {
     Component.onDestruction: console.log("Destruction Beginning!")
     Rectangle {
         Component.onDestruction: console.log("Nested Destruction Beginning!")
     }
 }

See also QtQml.


Method Documentation

object Component::createObject(Item parent, object properties)

Creates and returns an object instance of this component that will have the given parent and properties. The properties argument is optional. Returns null if object creation fails.

The object will be created in the same context as the one in which the component was created. This function will always return null when called on components which were not created in QML.

If you wish to create an object without setting a parent, specify null for the parent value. Note that if the returned object is to be displayed, you must provide a valid parent value or set the returned object's parent property, or else the object will not be visible.

If a parent is not provided to createObject(), a reference to the returned object must be held so that it is not destroyed by the garbage collector. This is true regardless of whether Item::parent is set afterwards, since setting the Item parent does not change object ownership; only the graphical parent is changed.

As of QtQuick 1.1, this method accepts an optional properties argument that specifies a map of initial property values for the created object. These values are applied before object creation is finalized. (This is more efficient than setting property values after object creation, particularly where large sets of property values are defined, and also allows property bindings to be set up before the object is created.)

The properties argument is specified as a map of property-value items. For example, the code below creates an object with initial x and y values of 100 and 200, respectively:

 var component = Qt.createComponent("Button.qml");
 if (component.status == Component.Ready)
     component.createObject(parent, {"x": 100, "y": 100});

Dynamically created instances can be deleted with the destroy() method. See Dynamic Object Management in QML for more information.

See also incubateObject().


object Component::incubateObject(Item parent, object properties, enumeration mode)

Creates an incubator for instance of this component. Incubators allow new component instances to be instantiated asynchronously and not cause freezes in the UI.

The parent argument specifies the parent the created instance will have. Omitting the parameter or passing null will create anobject with no parent. In this case, a reference to the created object must be maintained by the application of the object will eventually be garbage collected.

The properties argument is specified as a map of property-value items which will be set on the created object during its construction. mode may be Qt.Synchronous or Qt.Asynchronous and controls whether the instance is created synchronously or asynchronously. The default is asynchronously. In some circumstances, even if Qt.Synchronous is specified, the incubator may create the object asynchronously. This happens if the component calling incubateObject() is itself being created asynchronously.

All three arguments are optional.

If successful, the method returns an incubator, otherwise null. The incubator has the following properties:

  • status The status of the incubator. Valid values are Component.Ready, Component.Loading and Component.Error.
  • object The created object instance. Will only be available once the incubator is in the Ready status.
  • onStatusChanged Specifies a callback function to be invoked when the status changes. The status is passed as a parameter to the callback.
  • forceCompletion() Call to complete incubation synchronously.

The following example demonstrates how to use an incubator:

 var component = Qt.createComponent("Button.qml");

 var incubator = component.incubateObject(parent, { x: 10, y: 10 });
 if (incubator.status != Component.Ready) {
     incubator.onStatusChanged = function(status) {
         if (status == Component.Ready) {
             print ("Object", incubator.object, "is now ready!");
         }
     }
 } else {
     print ("Object", incubator.object, "is ready immediately!");
 }

Dynamically created instances can be deleted with the destroy() method. See Dynamic Object Management in QML for more information.

See also createObject().


string Component::errorString()

Returns a human-readable description of any errors.

The string includes the file, location, and description of each error. If multiple errors are present they are separated by a newline character.

If no errors are present, an empty string is returned.


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