QML Loader Element
The Loader item allows dynamically loading an Item-based subtree from a URL or Component. More...
Loader允许从URL或Component动态加载基于Item的子树。
Inherits Item
继承于Item
Properties
- item : Item
- progress : real
- source : url
- sourceComponent : Component
- status : enumeration
Signals
Detailed Description
Loader is used to dynamically load visual QML components. It can load a QML file (using the source property) or a Component object (using thesourceComponent property). It is useful for delaying the creation of a component until it is required: for example, when a component should be created on demand, or when a component should not be created unnecessarily for performance reasons.
Loader元素用来动态加载可见的QML组件。它可以用来加载一个QML文件(使用source属性),也可以用来记载一个组件(Component)对象(使用sourceComponent属性)。Loader主要是用来在必要时候创建组件。例如,当有需求时组件被创建,由于性能原因可能不需要被创建。
Here is a Loader that loads "Page1.qml" as a component when the MouseArea is clicked:
下面的例子, 当单击MouseArea时,Loader加载“page1.qml”:
- import QtQuick 1.0
- Item {
- width: 200; height: 200
- Loader { id: pageLoader }
- MouseArea {
- anchors.fill: parent
- onClicked: pageLoader.source = "Page1.qml"
- }
- }
import QtQuick 1.0
Item {
width: 200; height: 200
Loader { id: pageLoader }
MouseArea {
anchors.fill: parent
onClicked: pageLoader.source = "Page1.qml"
}
}
The loaded item can be accessed using the item property.
已经加载的项可以通过item属性来访问。
If the source or sourceComponent changes, any previously instantiated items are destroyed. Setting source to an empty string or setting sourceComponent toundefined destroys the currently loaded item, freeing resources and leaving the Loader empty.
当source或是sourceComponent属性发生变化时,之前加载的项都会被销毁,将source或是sourceComponent属性设置成空字符串可以销毁当前已经
加载的项,释放Loder所占用的资源。
(这里就等同于C++中的delete,但又有点不一样,因为delete之后就不可用了,如果要用需要再new出一个对象,而将source或是sourceComponent属性设置成空字符
串后,释放了资源,但仍然可以再重新给source或是sourceComponent属性赋值,这样还可以用。)
Loader sizing behavior
Loader is like any other visual item and must be positioned and sized accordingly to become visible.
Loader与其他可见item一样,需要设置位置的大小才可见。
- If an explicit size is not specified for the Loader, the Loader is automatically resized to the size of the loaded item once the component is loaded.
- 如果没有显式的指定大小,一旦组件被加载,Loader会自动设置组件的大小。
- If the size of the Loader is specified explicitly by setting the width, height or by anchoring, the loaded item will be resized to the size of the Loader.
- 如果Loader的大小被显式的指定width,height或anchor,被加载的item会被调整为Loader的大小。