能在qml組件需要的時(shí)候再創(chuàng)建,即延遲創(chuàng)建QML的時(shí)間,類似ios懶加載
main.qml
import QtQuick 2.1
import QtQuick.Window 2.0
import QtQuick.Controls 2.2
Window {
visible: true
width: 360
height: 360
Item
{
width: parent.width
height: parent.height
Loader
{
id:pageLoader
source: "qrc:/page1.qml"
focus: true
}
MouseArea
{
property bool isFirstClick:false
anchors.fill: parent
onClicked:
{
if( isFirstClick)
{
pageLoader.source = "qrc:/page1.qml"
}
else{pageLoader.source = "qrc:/page2.qml"}
isFirstClick = ! isFirstClick
}
}
}
}
page1.qml
import QtQuick 2.0
Rectangle
{
width: 360
height: 100
color: "red"
focus: true
Text {
id: txt
text: qsTr("Page1")
anchors.fill: parent
}
}
page2.qml
import QtQuick 2.0
Rectangle
{
width: 360
height: 100
color: "blue"
focus: true
Text {
id: txt
text: qsTr("Page2")
anchors.fill: parent
}
}
點(diǎn)擊窗口空白區(qū)域切換page