Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

Loader QML Element

The Loader item allows dynamically loading an Item-based subtree from a URL or Component.More...

Since: Qt 4.7
Inherits:

Item

Properties

Signals

Detailed Description

Loader is used to dynamically load visual QML components. It can load a QML file (using thesource property) or aComponent 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.

Here is a Loader that loads "Page1.qml" as a component when theMouseArea is clicked:

import QtQuick 1.0Item {width:200;height:200Loader {id:pageLoader }MouseArea {anchors.fill:parentonClicked:pageLoader.source="Page1.qml"    }}

The loaded item can be accessed using theitem property.

If thesource orsourceComponent changes, any previously instantiated items are destroyed. Settingsource to an empty string or settingsourceComponent toundefined destroys the currently loaded item, freeing resources and leaving the Loader empty.

Loader sizing behavior

Loader is like any other visual item and must be positioned and sized accordingly to become visible.

  • 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.
  • 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.

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.qmlsizeitem.qml
import QtQuick 1.0Item {width:200;height:200Loader {// Explicitly set the size of the Loader to the parent item's sizeanchors.fill:parentsourceComponent:rect    }Component {id:rectRectangle {width:50height:50color:"red"        }    }}
import QtQuick 1.0Item {width:200;height:200Loader {// position the Loader in the center of the parentanchors.centerIn:parentsourceComponent:rect    }Component {id:rectRectangle {width:50height:50color:"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.

Receiving signals from loaded items

Any signals emitted from the loaded item can be received using theConnections element. For example, the followingapplication.qml loadsMyItem.qml, and is able to receive themessage signal from the loaded item through aConnections object:

application.qmlMyItem.qml
import QtQuick 1.0Item {width:100;height:100Loader {id:myLoadersource:"MyItem.qml"    }Connections {target:myLoader.itemonMessage:console.log(msg)    }}
import QtQuick 1.0Rectangle {id:myItem   signalmessage(string msg)width:100;height:100MouseArea {anchors.fill:parentonClicked:myItem.message("clicked!")   }}

Alternatively, sinceMyItem.qml is loaded within the scope of the Loader, it could also directly call any function defined in the Loader or its parentItem.

Focus and key events

Loader is a focus scope. Itsfocus property must be set totrue for any of its children to get theactive focus. (Seethe focus documentation page for more details.) Any key events received in the loaded item should likely also beaccepted so they are not propagated to the Loader.

For example, the followingapplication.qml loadsKeyReader.qml when theMouseArea is clicked. Notice thefocus property is set totrue for the Loader as well as theItem in the dynamically loaded object:

application.qmlKeyReader.qml
import QtQuick 1.0Rectangle {width:200;height:200Loader {id:loaderfocus:true    }MouseArea {anchors.fill:parentonClicked:loader.source="KeyReader.qml"    }Keys.onPressed: {console.log("Captured:",event.text);    }}
import QtQuick 1.0Item {Item {focus:trueKeys.onPressed: {console.log("Loaded item captured:",event.text);event.accepted=true;        }    }}

OnceKeyReader.qml is loaded, it accepts key events and setsevent.accepted totrue so that the event is not propagated to the parentRectangle.

See alsoDynamic Object Creation.

Property Documentation

item :Item

This property holds the top-level item that is currently loaded.


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.

See alsostatus.


source :url

This property holds the URL of the QML component to instantiate.

Note the QML component must be anItem-based component. The loader cannot load non-visual components.

To unload the currently loaded item, set this property to an empty string, or setsourceComponent toundefined. Settingsource to a new URL will also cause the item created by the previous URL to be unloaded.

See alsosourceComponent,status, andprogress.


sourceComponent :Component

This property holds theComponent to instantiate.

Item {Component {id:redSquareRectangle {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 orundefined.

See alsosource andprogress.


status :enumeration

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

  • Loader.Null - no QML source has been set
  • Loader.Ready - the QML source has been loaded
  • Loader.Loading - the QML source is currently being loaded
  • Loader.Error - an error occurred while loading the QML source

Use this status to provide an update or respond to the status change in some way. For example, you could:

  • Trigger a state change:
    State {name:'loaded';when:loader.status==Loader.Ready }
  • Implement anonStatusChanged signal handler:
    Loader {id:loaderonStatusChanged:if (loader.status==Loader.Ready)console.log('Loaded')}
  • Bind to the status value:
    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, theonLoaded will still be invoked.

See alsoprogress.


Signal Documentation

onLoaded()

This handler is called when thestatus becomesLoader.Ready, or on successful initial load.


© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp