QML笔记Loader的使用方法
Loader简介
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.
祥见Loader QML Type | Qt Quick 5.9
译文:
loader用于动态加载qml组件。
加载程序可以加载qml文件(使用source属性)或组件对象(使用source component属性)。将组件的创建延迟到需要时非常有用:例如,当应按需创建组件时,或者当不应因性能原因而不必要地创建组件时。
为什么要使用Loader
在使用QML开发复杂界面时,应为需要实现一个系统下各个子系统的滑动效果。如果不使用动态加载,每一个子系统界面进行数据交互会导致系统卡顿或者浪费大量不比要的资源。
之前使用了Loader的sourceComponent来实现,但是会导致程序在点击进入该界面时卡顿严重,具体原因我猜测是QML在加载界面时会把所有Component组件全部加载一遍。因此使用Loader的source直接动态加载qml文件。
Loader的sourceComponent用法
在同一个qml文件中定义
// 申明Loader
Loader{
id:componetLoader
sourceComponent: undefined
}
//创建组件
Component{
id:resectionSystemCom
Rectangle{
x:577;y: