QML23、常规组件Loaer

Loader是用来动态加载qml components的,这样就可以在你需要相应的component时才创建它,即延迟创建 qml 的控件,提高运行速度。如果页面中需要加载的组件过多时,Loader的好处尤为明显。

1.加载方式

Loader可以通过两种方式来加载components,一种是通过source属性来加载一个qml文件(例子1),另一种是通过sourceComponent属性来加载(例子2)。这两种方式都是在鼠标click root后才加载的。
例子1:
import QtQuick 2.2
Rectangle {
id:root
width: 360
height: 360


Loader
{
    id:loader
}


MouseArea {
    anchors.fill: parent
    onClicked: {
        loader.source = "Page.qml";
    }
}
}

例子2:
import QtQuick 2.2
Rectangle {
id:root
width: 360
height: 360


Loader
{
    id:loader
}


MouseArea {
    anchors.fill: parent
    onClicked: {
        loader.sourceComponent = component1
    }
}

你可能感兴趣的:(QML,前端,javascript,html)