Qt QML change item of a page dynamically

Solution 1:

You can define an alias to the Loader's item property inside BasePage, like that:

property alias contentItem: loader.item

And refer to it instead of content item within DerivedPage.


Putting it all together:

// BasePage.qml
Item {
    property alias content: loader.sourceComponent
    property alias contentItem: loader.item
    signal topBarLeftButtonClicked()
    Loader { id: loader }
}

// DerivedPage.qml
BasePage {
    onTopBarLeftIconClicked: { contentItem.text = "clicked" }
    content: Component { TextField { } }
}