Allows dynamic loading of a subtree from a URL or Component. 更多...
import 语句: | import QtQuick 2.15 |
继承: |
Loader is used to dynamically load QML components.
Loader can load a QML file (using the source property) or a Component object (using the sourceComponent 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.
Here is a Loader that loads "Page1.qml" as a component when the MouseArea is clicked:
import QtQuick 2.0 Item { width: 200; height: 200 Loader { id: pageLoader } MouseArea { anchors.fill: parent onClicked: pageLoader.source = "Page1.qml" } }
The loaded object can be accessed using the item 特性。
若
source
or
sourceComponent
changes, any previously instantiated items are destroyed. Setting
source
to an empty string or setting
sourceComponent
to
undefined
destroys the currently loaded object, freeing resources and leaving the Loader empty.
If the source component is not an Item type, Loader does not apply any special sizing rules. When used to load visual types, Loader applies the following sizing rules:
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.
sizeloader.qml | sizeitem.qml |
import QtQuick 2.0 Item { width: 200; height: 200 Loader { // Explicitly set the size of the // Loader to the parent item's size anchors.fill: parent sourceComponent: rect } Component { id: rect Rectangle { width: 50 height: 50 color: "red" } } } |
import QtQuick 2.0 Item { width: 200; height: 200 Loader { // position the Loader in the center // of the parent anchors.centerIn: parent sourceComponent: rect } Component { id: rect Rectangle { width: 50 height: 50 color: "red" } } } |
The red rectangle will be sized to the size of the root item. | The red rectangle will be 50x50, centered in the root item. |
Any signals emitted from the loaded object can be received using the
Connections
type. For example, the following
application.qml
loads
MyItem.qml
, and is able to receive the
message
signal from the loaded item through a
Connections
对象:
application.qml | MyItem.qml |
import QtQuick 2.0 Item { width: 100; height: 100 Loader { id: myLoader source: "MyItem.qml" } Connections { target: myLoader.item onMessage: console.log(msg) } } |
import QtQuick 2.0 Rectangle { id: myItem signal message(string msg) width: 100; height: 100 MouseArea { anchors.fill: parent onClicked: myItem.message("clicked!") } } |
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
.
Loader is a focus scope. Its
focus
property must be set to
true
for any of its children to get the
active focus
。(见
Qt Quick 中的键盘聚焦
for more details.) Any key events received in the loaded item should likely also be
accepted
so they are not propagated to the Loader.
例如,以下
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:
application.qml | KeyReader.qml |
import QtQuick 2.0 Rectangle { width: 200; height: 200 Loader { id: loader focus: true } MouseArea { anchors.fill: parent onClicked: { loader.source = "KeyReader.qml" } } Keys.onPressed: { console.log("Captured:", event.text); } } |
import QtQuick 2.0 Item { Item { focus: true Keys.onPressed: { console.log("KeyReader captured:", event.text); event.accepted = true; } } } |
一旦
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
.
由于
QtQuick 2.0
, Loader can also load non-visual components.
In some cases you may wish to use a Loader within a view delegate to improve delegate loading performance. This works well in most cases, but there is one important issue to be aware of related to the creation context of a Component.
在以下范例中,
index
context property inserted by the
ListView
into
delegateComponent
's context will be inaccessible to Text, as the Loader will use the creation context of
myComponent
as the parent context when instantiating it, and
index
does not refer to anything within that context chain.
Item { width: 400 height: 400 Component { id: myComponent Text { text: index } //fails } ListView { anchors.fill: parent model: 5 delegate: Component { id: delegateComponent Loader { sourceComponent: myComponent } } } }
In this situation we can either move the component inline,
delegate: Component { Loader { sourceComponent: Component { Text { text: index } //okay } } }
into a separate file,
delegate: Component { Loader { source: "MyComponent.qml" //okay } }
or explicitly set the required information as a property of the Loader (this works because the Loader sets itself as the context object for the component it is loading).
Item { width: 400 height: 400 Component { id: myComponent Text { text: modelIndex } //okay } ListView { anchors.fill: parent model: 5 delegate: Component { Loader { property int modelIndex: index sourceComponent: myComponent } } } }
另请参阅 动态对象创建 .
active : bool |
此特性是
true
if the Loader is currently active. The default value for this property is
true
.
If the Loader is inactive, changing the source or sourceComponent will not cause the item to be instantiated until the Loader is made active.
Setting the value to inactive will cause any item loaded by the loader to be released, but will not affect the source or sourceComponent .
The
status
of an inactive loader is always
Null
.
另请参阅 source and sourceComponent .
asynchronous : bool |
This property holds whether the component will be instantiated asynchronously. By default it is
false
.
When used in conjunction with the source property, loading and compilation will also be performed in a background thread.
Loading asynchronously creates the objects declared by the component across multiple frames, and reduces the likelihood of glitches in animation. When loading asynchronously the status will change to Loader.Loading. Once the entire component has been created, the item will be available and the status will change to Loader.Ready.
Changing the value of this property to
false
while an asynchronous load is in progress will force immediate, synchronous completion. This allows beginning an asynchronous load and then forcing completion if the Loader content must be accessed before the asynchronous load has completed.
To avoid seeing the items loading progressively set
visible
appropriately, e.g.
Loader { source: "mycomponent.qml" asynchronous: true visible: status == Loader.Ready }
Note that this property affects object instantiation only; it is unrelated to loading a component asynchronously via a network.
item : QtObject |
This property holds the top-level object that is currently loaded.
由于
QtQuick 2.0
, Loader can load any object type.
progress : 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.
另请参阅 status .
source : url |
This property holds the URL of the QML component to instantiate.
由于
QtQuick 2.0
, Loader is able to load any type of object; it is not restricted to Item types.
To unload the currently loaded object, 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.
另请参阅 sourceComponent , status ,和 progress .
sourceComponent : Component |
此特性保持 Component to instantiate.
Item { Component { id: redSquare Rectangle { color: "red"; width: 10; height: 10 } } Loader { sourceComponent: redSquare } Loader { sourceComponent: redSquare; x: 10 } }
To unload the currently loaded object, set this property to
undefined
.
由于
QtQuick 2.0
, Loader is able to load any type of object; it is not restricted to Item types.
status : enumeration |
This property holds the status of QML loading. It can be one of:
Use this status to provide an update or respond to the status change in some way. For example, you could:
State { name: 'loaded'; when: loader.status == Loader.Ready }
onStatusChanged
signal handler:
Loader { id : loader onStatusChanged : if ( loader . status == Loader . Ready ) console . log ( 'Loaded' ) }
Text { text: loader.status == Loader.Ready ? 'Loaded' : 'Not loaded' }
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.
另请参阅 progress .
此信号发射当
status
becomes
Loader.Ready
, or on successful initial load.
注意:
相应处理程序是
onLoaded
.
对象 setSource ( url source , 对象 properties ) |
Creates an object instance of the given source component that will have the given properties 。 properties argument is optional. The instance will be accessible via the item property once loading and instantiation is complete.
若
active
特性为
false
at the time when this function is called, the given
source
component will not be loaded but the
source
and initial
properties
will be cached. When the loader is made
active
, an instance of the
source
component will be created with the initial
properties
set.
Setting the initial property values of an instance of a component in this manner will not trigger any associated Behavior 。
Note that the cached properties will be cleared if the source or sourceComponent is changed after calling this function but prior to setting the loader active .
范例:
// ExampleComponent.qml import QtQuick 2.0 Rectangle { id: rect color: "red" width: 10 height: 10 Behavior on color { NumberAnimation { target: rect property: "width" to: (rect.width + 20) duration: 0 } } } |
// example.qml import QtQuick 2.0 Item { Loader { id: squareLoader onLoaded: console.log(squareLoader.item.width); // prints [10], not [30] } Component.onCompleted: { squareLoader.setSource("ExampleComponent.qml", { "color": "blue" }); // will trigger the onLoaded code when complete. } } |