
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
The Component element encapsulates a QML component definition.More...
| Since: | Qt 4.7 |
| Instantiates: | QDeclarativeComponent |
Components are reusable, encapsulated QML elements with well-defined interfaces.
Components are often defined bycomponent files - that is,.qml files. TheComponent 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 multipleLoader objects. It contains a single item, aRectangle:
import QtQuick 1.0Item {width:100;height:100Component {id:redSquareRectangle {color:"red"width:10height:10 } }Loader {sourceComponent:redSquare }Loader {sourceComponent:redSquare;x:20 }}
Notice that while aRectangle by itself would be automatically rendered and displayed, this is not the case for the above rectangle because it is defined inside aComponent. 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 twoLoader objects).
Defining aComponent 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, aComponent definition contains a single top level item (which in the above example is aRectangle) and cannot define any data outside of this item, with the exception of anid (which in the above example isredSquare).
TheComponent element is commonly used to provide graphical components for views. For example, theListView::delegate property requires aComponent to specify how each list item is to be displayed.
Component objects can also be created dynamically using Qt.createComponent().
This property holds the status of component loading. It can be one of:
url :url |
The component URL. This is the URL that was used to construct the component.
Emitted after component "startup" has completed. This can be used to execute script code at startup, once the full QML environment has been established.
TheComponent::onCompleted attached property can be applied to any element. The order of running theonCompleted scripts is undefined.
Emitted as the component begins destruction. This can be used to undo work done in theonCompleted signal, or other imperative code in your application.
TheComponent::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 theonDestruction scripts is undefined.
Rectangle {Component.onDestruction:console.log("Destruction Beginning!")Rectangle {Component.onDestruction:console.log("Nested Destruction Beginning!") }}
See alsoQtDeclarative.
objectcreateObject(Item parent,object properties) |
Creates and returns an object instance of this component that will have the givenparent andproperties. Theproperties 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, specifynull for theparent value. Note that if the returned object is to be displayed, you must provide a validparent value or set the returned object'sparent property, or else the object will not be visible.
If aparent 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 whetherItem::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 optionalproperties 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.)
Theproperties argument is specified as a map of property-value items. For example, the code below creates an object with initialx andy values of 100 and 200, respectively:
varcomponent =Qt.createComponent("Button.qml");if (component.status==Component.Ready)component.createObject(parent, {"x":100, "y":100});
Dynamically created instances can be deleted with thedestroy() method. SeeDynamic Object Management in QML for more information.
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.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.