QML Loader Element

QML Loader Element用于动态加载基于Item的子树,可从URL或Component加载。适用于延迟组件创建,如按需创建或性能考虑。Loader的大小行为、接收加载项信号、焦点及键盘事件管理等方面进行了详细描述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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”:

[javascript] view plain copy print ?
  1. import QtQuick 1.0  
  2.   
  3. Item {  
  4.     width: 200; height: 200  
  5.   
  6.     Loader { id: pageLoader }  
  7.   
  8.     MouseArea {  
  9.         anchors.fill: parent  
  10.         onClicked: pageLoader.source = "Page1.qml"  
  11.     }  
  12. }  
 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
 
     
     
[javascript] view plain copy print ?
  1. import QtQuick 1.0  
  2.   
  3.  Item {  
  4.      width: 200; height: 200  
  5.   
  6.      Loader {  
  7.          // Explicitly set the size of the Loader to the parent item's size   
  8.          anchors.fill: parent  
  9.          sourceComponent: rect  
  10.      }  
  11.   
  12.      Component {  
  13.          id: rect  
  14.          Rectangle {  
  15.              width: 50  
  16.              height: 50  
  17.              color: "red"  
  18.          }  
  19.      }  
  20.  }  
import QtQuick 1.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"
         }
     }
 }


 
     
     
[javascript] view plain copy print ?
  1. import QtQuick 1.0  
  2.   
  3.  Item {  
  4.      width: 200; height: 200  
  5.   
  6.      Loader {  
  7.          // position the Loader in the center of the parent   
  8.          anchors.centerIn: parent  
  9.          sourceComponent: rect  
  10.      }  
  11.   
  12.      Component {  
  13.          id: rect  
  14.          Rectangle {  
  15.              width: 50  
  16.              height: 50  
  17.              color: "red"  
  18.          }  
  19.      }  
  20.  }  
import QtQuick 1.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.

这红矩形的大小是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
 
     
     
[javascript] view plain copy print ?
  1. import QtQuick 1.0  
  2.   
  3.  Item {  
  4.      width: 100; height: 100  
  5.   
  6.      Loader {  
  7.         id: myLoader  
  8.         source: "MyItem.qml"  
  9.      }  
  10.   
  11.      Connections {  
  12.          target: myLoader.item  
  13.          onMessage: console.log(msg)  
  14.      }  
  15.  }  
import QtQuick 1.0

 Item {
     width: 100; height: 100

     Loader {
        id: myLoader
        source: "MyItem.qml"
     }

     Connections {
         target: myLoader.item
         onMessage: console.log(msg)
     }
 }


 
     
     
[javascript] view plain copy print ?
  1. import QtQuick 1.0  
  2.   
  3.  Rectangle {  
  4.     id: myItem  
  5.     signal message(string msg)  
  6.   
  7.     width: 100; height: 100  
  8.   
  9.     MouseArea {  
  10.         anchors.fill: parent  
  11.         onClicked: myItem.message("clicked!")  
  12.     }  
  13.  }  
import QtQuick 1.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.

相反,由于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
 
     
     
[javascript] view plain copy print ?
  1. import QtQuick 1.0  
  2.   
  3.  Rectangle {  
  4.      width: 200; height: 200  
  5.   
  6.      Loader {  
  7.          id: loader  
  8.          focus: true  
  9.      }  
  10.   
  11.      MouseArea {  
  12.          anchors.fill: parent  
  13.          onClicked: loader.source = "KeyReader.qml"  
  14.      }  
  15.   
  16.      Keys.onPressed: {  
  17.          console.log("Captured:", event.text);  
  18.      }  
  19.  }  
import QtQuick 1.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);
     }
 }


 
     
     
[javascript] view plain copy print ?
  1. import QtQuick 1.0  
  2.   
  3.  Item {  
  4.      Item {  
  5.          focus: true  
  6.          Keys.onPressed: {  
  7.              console.log("Loaded item captured:", event.text);  
  8.              event.accepted = true;  
  9.          }  
  10.      }  
  11.  }  
import QtQuick 1.0

 Item {
     Item {
         focus: true
         Keys.onPressed: {
             console.log("Loaded item captured:", event.text);
             event.accepted = true;
         }
     }
 }


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 sourceComponentstatus, and progress.


sourceComponent : Component

This property holds the Component to instantiate.

sourceComponent属性持有的是需要加载的组件

[javascript] view plain copy print ?
  1. Item {  
  2.     Component {  
  3.         id: redSquare  
  4.         Rectangle { color: "red"; width: 10; height: 10 }  
  5.     }  
  6.   
  7.     Loader { sourceComponent: redSquare }  
  8.     Loader { sourceComponent: redSquare; x: 10 }  
  9. }  
 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.
如果想卸载当前已经加载的组件时,只要将sourceComponent设置成空字符串或是设置成undefined。

See also source and progress.


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:
     State { name: 'loaded'; when: loader.status == Loader.Ready }
  • Implement an onStatusChanged signal handler:
         
         
    [javascript] view plain copy print ?
    1. Loader {  
    2.     id: loader  
    3.     onStatusChanged: if (loader.status == Loader.Ready) console.log('Loaded')  
    4. }  
     Loader {
         id: loader
         onStatusChanged: 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, the onLoaded will still be invoked.

注意如果source是本地文件,status会初始化为Ready(或Error)。这种情况下没有onStatusChanged 信号,这onLoaded仍然会被调用。

See also progress.


Signal Documentation

Loader::onLoaded ()

This handler is called when the status becomes Loader.Ready, or on successful initial load.

当status属性值变为Loader.Ready或成功初始加载时,会调用Loader::onLoader()。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值