There’s two issues with your code.
- You should be using
parent = loader.item
, notparent = loader
. You don’t want the Loader to be your parent, but the item that it has loaded. - The Component that you’re adding to the Loader is a Window, which is not a QQuickItem. You will get an error when trying to assign a Window as the visual parent of an Item. You can get around that by
creating an Item inside the window and exposing that through a propertyusing Window’s contentItem property, like this:
Loader
{
id: loader
}
Component
{
id: rect
Window
{
id: ppp
visible: true
width: 164
height: 148
title: qsTr("Hello World")
}
}
Rectangle
{
anchors.fill: parent
color: "blue"
Rectangle
{
id: leftArea
height: 100
width: 100
color: "red"
MouseArea
{
anchors.fill: parent
onClicked:
{
loader.sourceComponent = rect
leftArea.parent = loader.item.contentItem
}
}
}
}
CLICK HERE to find out more related problems solutions.