Component QML Type

  • Import Statement: import QtQml

  • Instantiates:: QQmlComponent

  • Group: Component is part of qml-utility-elements

Detailed Description

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

Components are often defined by component files - that is, .qml files. The Component type 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:

 
Sélectionnez
import QtQuick 2.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 types 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). Because Component is not derived from Item, you cannot anchor anything to it.

Defining a Component is similar to defining a QML document. A QML document has a single top-level item that defines the behavior and properties of that component, and cannot define properties or behavior 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 type 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().

Creation Context

The creation context of a Component corresponds to the context where the Component was declared. This context is used as the parent context (creating a context hierarchy) when the component is instantiated by an object such as a ListView or a Loader.

In the following example, comp1 is created within the root context of MyItem.qml, and any objects instantiated from this component will have access to the ids and properties within that context, such as internalSettings.color. When comp1 is used as a ListView delegate in another context (as in main.qml below), it will continue to have access to the properties of its creation context (which would otherwise be private to external users).

MyItem.qml

 
Sélectionnez
Item {
    property Component mycomponent: comp1

    QtObject {
        id: internalSettings
        property color color: "green"
    }

    Component {
        id: comp1
        Rectangle { color: internalSettings.color; width: 400; height: 50 }
    }
}

main.qml

 
Sélectionnez
ListView {
    width: 400; height: 400
    model: 5
    delegate: myItem.mycomponent    //will create green Rectangles

    MyItem { id: myItem }
}

It is important that the lifetime of the creation context outlive any created objects. See Maintaining Dynamically Created Objects for more details.

Property Documentation

 

[read-only] progress : real

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

[read-only] status : enumeration

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

  • 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-only] url : url

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

Attached Signal Documentation

 

completed()

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

The onCompleted signal handler can be declared on any object. The order of running the handlers is undefined.

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

The corresponding handler is onCompleted.

destruction()

Emitted as the object begins destruction. This can be used to undo work done in response to the completed() signal, or other imperative code in your application.

The onDestruction signal handler can be declared on any object. The order of running the handlers is undefined.

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

The corresponding handler is onDestruction.

See Also

See also Qt QML

Method Documentation

 

QtObject createObject(QtObject 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