Qt Reference Documentation

QML Component Element

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

Component instantiates the C++ class QDeclarativeComponent

  • List of all members, including inherited members
  • 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 allows components to be defined within QML items rather than in a separate 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 top level Rectangle item:

     import Qt 4.7
    
     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).

    A Component cannot contain anything other than an id and a single top level item. While the id is optional, the top level item is not; you cannot define an empty component.

    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 dynamically created 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 QtDeclarative.


    Method Documentation

    object Component::createObject ( Item parent )

    Creates and returns an object instance of this component that will have the given parent. 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.

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


    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.


    X

    Thank you for giving your feedback.

    Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.