QML Loader使用

能在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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容