能在qml组件需要的时候再创建,即延迟创建QML的时间,类似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
}
}
点击窗口空白区域切换page