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的大小。
In both scenarios the size of the item and the Loader are identical. This ensures that anchoring to the Loader is equivalent to anchoring to the loaded item.
两者情况下item和Loader的大小是相同的。这确保了Loader的anchoring等于加载项的anchoring。
sizeloader.qml | sizeitem.qml |
The red rectangle will be sized to the size of the root item. 这红矩形的大小会变成根项的大小。 | The red rectangle will be 50x50, centered in the root item. 这红矩形的大小是50x50,位于根项中间。 |
Receiving signals from loaded items
Any signals emitted from the loaded item can be received using the Connections element. For example, the following application.qml loads MyItem.qml, and is able to receive the message signal from the loaded item through a Connections object:
任何从已经加载项中发出的信号都可以使用Connections元素来接收。在下面的例子中,application.qml中加载了MyItem.qml,通过一个Connections对象能从已加载项那接收message signal。
application.qml | MyItem.qml |
Alternatively, since MyItem.qml is loaded within the scope of the Loader, it could also directly call any function defined in the Loader or its parent Item.
相反,由于MyItem.qml被加载到Loader中,所以它能直接调用在Loader或是Loader的父项中定义的方法。
Focus and key events
Loader is a focus scope. Its focus property must be set to true for any of its children to get the active focus. (See the focus documentation page for more details.) Any key events received in the loaded item should likely also be accepted so they are not propagated to the Loader.
Loader是一个焦点区域,如果想让Loader的子item获得活动的焦点,必须将Loader的focus属性设置为真。Loader中加载的组件应该接受任何收到的键盘事件,这样键盘事件就不会抛给Loader处理。
For example, the following application.qml loads KeyReader.qml when the MouseArea is clicked. Notice the focus property is set to true for the Loader as well as the Item in the dynamically loaded object:
在下面的例子中, 当单击MouseArea时,application.qml加载KeyReader.qml文件。注意Loader的focus属性和KeyReader.qml中的item的focus属性均设置为真。
application.qml | KeyReader.qml |
Once KeyReader.qml is loaded, it accepts key events and sets event.accepted to true so that the event is not propagated to the parent Rectangle.
一旦KeyReader.qml被加载,它会接受所有的键盘事件,将event.accepted属性设置为真,这样事件就不会上抛给Rectangle处理。
See also Dynamic Object Creation.
Property Documentation
read-onlyitem : Item |
This property holds the top-level item that is currently loaded.
这个属性持有了当前加载的最顶层item.
read-onlyprogress : real |
This property holds the progress of loading QML data from the network, from 0.0 (nothing loaded) to 1.0 (finished). Most QML files are quite small, so this value will rapidly change from 0 to 1.
这个属性持有的是从网络加载QML数据的进度。从0.0(没有加载时),到1.0(加载完毕时)。多数QML文件相当小,所以从0到1的值变化非常快。
See also status.
source : url |
This property holds the URL of the QML component to instantiate.
需要加载的QML组件的路径。
Note the QML component must be an Item-based component. The loader cannot load non-visual components.
注意QML组件必须是基于item的组件。loader不能加载非可视化组件。
To unload the currently loaded item, set this property to an empty string, or set sourceComponent to undefined. Setting source to a new URL will also cause the item created by the previous URL to be unloaded.
卸载当前已经加载的组件时,只要将source设置成空字符串,或是将sourceComponent设置成undefined。设置source为new URL也会前URL创建的item被卸载。
See also sourceComponent, status, and progress.
sourceComponent : Component |
This property holds the Component to instantiate.
sourceComponent属性持有的是需要加载的组件
- Item {
- Component {
- id: redSquare
- Rectangle { color: "red"; width: 10; height: 10 }
- }
- Loader { sourceComponent: redSquare }
- Loader { sourceComponent: redSquare; x: 10 }
- }
Item {
Component {
id: redSquare
Rectangle { color: "red"; width: 10; height: 10 }
}
Loader { sourceComponent: redSquare }
Loader { sourceComponent: redSquare; x: 10 }
}
To unload the currently loaded item, set this property to an empty string or undefined.
read-onlystatus : enumeration |
This property holds the status of QML loading. It can be one of:
status表明加载的状态。有以下可选值:
- Loader.Null - no QML source has been set
- Loader.Null—— 表明没有设置需要加载的QML数据。
- Loader.Ready - the QML source has been loaded
- Loader.Ready ——表明QML数据已经加载完毕。
Loader.Loading - the QML source is currently being loaded - Loader.Loading ——表明正在加载QML数据。
- Loader.Error - an error occurred while loading the QML source
- Loader.Error ——表明加载QML数据时出错。
Use this status to provide an update or respond to the status change in some way. For example, you could:
使用status可以提供更新或响应status的变化。
- Trigger a state change:
- Implement an onStatusChanged signal handler:
- Bind to the status value:
Note that if the source is a local file, the status will initially be Ready (or Error). While there will be no onStatusChanged signal in that case, the onLoaded will still be invoked.
注意如果source是本地文件,status会初始化为Ready(或Error)。这种情况下没有onStatusChanged 信号,这onLoaded仍然会被调用。
See also progress.
Signal Documentation
This handler is called when the status becomes Loader.Ready, or on successful initial load.
当status属性值变为Loader.Ready或成功初始加载时,会调用Loader::onLoader()。