Qt 4.8
Component Class Reference

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

Detailed Description

The Component element encapsulates a QML component definition.

Since
4.7

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 :

import QtQuick 1.0
Item {
width: 100; height: 100
Component {
id: redSquare
Rectangle {
color: "red"
width: 10
height: 10
}
}
Loader { sourceComponent: redSquare }
Loader { sourceComponent: redSquare; x: 20 }
}

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 QML:Qt::createComponent() "Qt.createComponent()".


The documentation for this class was generated from the following file: