打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
QML Component Element
userphoto

2012.02.21

关注

QML Component Element

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

Component元素封装了一个QML组件的定义。

Component instantiates the C++ class QDeclarativeComponent

Component实例化了C++类QDeclarativeComponent

Properties

Attached Signals

Methods

Detailed Description

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

Component具有可重用性,封装好的具有良好接口定义的QML元素

Components are often defined by component files - that is, .qml files. The Component element essentially allows QML components to be defined inline, within aQML 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.

通常来说component被定义在组件文件中——就是.qml文件。Component 元素允许QML组件被定义在内部,在一个QML文档而不是独立的QML文件。这有利于在文件中重用小的组件,或者有利于定义一个组件逻辑上属于这个文件里其他QML组件。

For example, here is a component that is used by multiple Loader objects. It contains a single item, a Rectangle:

例如,这里component被多个Loader对象使用。它包含一个单个item,一个Rectangle。

 

[javascript] view plaincopy?
  1. import QtQuick 1.0  
  2.   
  3.  Item {  
  4.      width: 100; height: 100  
  5.   
  6.      Component {  
  7.          id: redSquare  
  8.   
  9.          Rectangle {  
  10.              color: "red"  
  11.              width: 10  
  12.              height: 10  
  13.          }  
  14.      }  
  15.   
  16.      Loader { sourceComponent: redSquare }  
  17.      Loader { sourceComponent: redSquare; x: 20 }  
  18.  }  


 

 

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 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 two Loader objects).

注意虽然Rectangle元素会自动渲染和显示,但对于上例中的情况则有点特殊。上面的例子中rectangle是在一个组件(component)中定义的。组件包含的一些QML元素,好像这些元素在另外一个QML文件中定义似地,只要当需要的时候才会执行加载。


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 isredSquare).

定义一个Component类似于定义一个QML文档。一个QML文档有单个的顶层item,定义了这个组件的行为和属性,不能在这个顶层item外面定义行为和属性。同样,Component定义包含了一个单独的顶层item(上面例子中是Rectangle),不能在这个item外面定义任何数据,除了id(上面的例子是redSquare)。

(一个组件只能包含一个id属性和一个顶层的item,其中id是可选的但item必须定义,不能定义一个空的组件。)

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.

组件通常是从视图的提取出的公共部分。例如ListView::delegate属性需要指定一个组件来绘制列表的每一项。

Component objects can also be created dynamically using Qt.createComponent().

组件对象还可以通过调用Qt.createComponent()来动态创建一个组件对象。

Property Documentation

read-onlyprogress : real

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

表明加载组件的进度。0.0表示没有加载,1.0表示加载完毕。

read-onlystatus : enumeration

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

status表明组件加载的状态。有以下可选值:

Component.Null - no data is available for the component

Component.Null—— 表明没有设置需要加载的QML数据。

Component.Ready - the component has been loaded, and can be used to create instances.

Component.Ready ——表明QML数据已经加载完毕,可以用于创建实例。
Component.Loading - the component is currently being loaded

Component.Loading 表明正在加载QML数据。

Component.Error - an error occurred while loading the component. Calling errorString() will provide a human-readable description of any errors.

Component.Error—— 表明加载QML数据时出错,调用errorString() 可以提供错误提示。


read-onlyurl : url

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

组件的URL可被用于构建这个组件。


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.

当组件启动完成后onCompleted()信号会被发送。我们可以在启动后执行一些代码。 附属属性Component::onCompleted 能被任何元素应用。onCompleted 的运行顺序是未定的。

[javascript] view plaincopy?
  1. Rectangle {  
  2.     Component.onCompleted: console.log("Completed Running!")  
  3.     Rectangle {  
  4.         Component.onCompleted: console.log("Nested Completed Running!")  
  5.     }  
  6. }  

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.

当组件析构时会发送onDestruction ()信号。可用于执行undo操作,或其它必需的代码。

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.

附属属性Component::onDestruction 可被其他任何代码应用。然而,它适用于这个组件的整体的析构,而不是指定对象的析构。onDestruction 运行顺序未定。

[javascript] view plaincopy?
  1. Rectangle {  
  2.     Component.onDestruction: console.log("Destruction Beginning!")  
  3.     Rectangle {  
  4.         Component.onDestruction: console.log("Nested Destruction Beginning!")  
  5.     }  
  6. }  

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.

创建一个以parent为父item的组件的对象实例病返回它。如果创建失败则返回null。

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.

被创建对象的上下文和这个组件是相同的,如果不是被创建在QML中,则返回null。

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.

如果你希望创建的对象不设置父对象,则指定parent的值为null。注意如果想让创建的组件显示,则需要指定一个合法的父item(非null的)或是手动为创建的对象设置一个合法的父item,否则创建的组件将不会显示。

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

动态创建的实例能被destroy() 方法删除。


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.

没有错误返回空。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
InfoQ: Spring2.5的新特性:第一部分
关于动态QML组件的几种用法 (转)
Introduction to the QML Language
如何优化Qt Qml开发性能?
HTC开发简介
React16源码解读:开篇带你搞懂几个面试考点
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服