Component QML Type

封装QML组件定义更多...

导入语句: import QtQml 2.2
Instantiates: QQmlComponent

属性

附加信号

方法

详细说明

组件是可重用的,使用定义良好的接口封装QML类型。

组件通常由组件文件定义 - 即.qml文件。组件类型本质上允许在QML文档内内联定义QML组件,而不是作为单独的QML文件。这可能有助于重用QML文件中的小组件,或者用于定义逻辑上属于文件中的其他QML组件的组件。

例如,这里是由多个Loader对象使用的组件。它包含单个控件,Rectangle


  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 }
  }

请注意,虽然Rectangle本身会自动呈现和显示,但上述矩形不是这样,因为它在组件中定义。组件封装了QML类型,就好像它们在单独的QML文件中定义,并且直到被请求(在这种情况下,两个Loader对象)才被加载。因为Component不是从Item派生的,所以不能将任何东西锚定到它。

定义组件类似于定义QML文档QML文档具有单个顶级项目,其定义该组件的行为和属性,并且不能在该顶级项目之外定义属性或行为。同样,组件定义包含单个顶级项(在上例中为Rectangle),但不能在此项之外定义任何数据,例外id(在上例中为redSquare)。

组件类型通常用于为视图提供图形组件。例如,ListView :: delegate属性需要组件来指定如何显示每个列表项。

组件对象也可以使用Qt.createComponent()动态创建。

创建上下文

组件的创建上下文对应于声明组件的上下文。当组件由对象(如ListView或Loader)实例化时,此上下文用作父上下文(创建上下文层次结构)。

在下面的示例中,在MyItem.qml的根上下文中创建了comp1,从该组件实例化的任何对象都可以访问该上下文中的id和属性,例如internalSettings。颜色当在另一个上下文中使用comp1作为ListView代理时(如在main.qml中),它将继续访问其创建上下文的属性(对外部用户是私有的)。

MyItem.qml

  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

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

      MyItem { id: myItem }
  }

财产文件

progress : real

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


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.

url : url

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


附加信号文档

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 corresponding handler is onCompleted. It can be declared on any object. The order of running the onCompleted handlers is undefined.


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


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 corresponding handler is onDestruction. It can be declared on any object. The order of running the onDestruction handlers is undefined.


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

See also Qt QML.


Method Documentation

object 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 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, otherwise 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, because 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 the 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 (using Qt.binding) 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 100, 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 QML Object Creation from JavaScript for more information.

See also incubateObject().


string errorString()

Returns a human-readable description of any error.

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.


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

Creates an incubator for an instance of this component. Incubators allow new component instances to be instantiated asynchronously and do 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 an object with no parent. In this case, a reference to the created object must be held so that it is not destroyed by the garbage collector.

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 asynchronous. 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 QML Object Creation from JavaScript for more information.

See also createObject().