how can i make a component the parent of some qtquick object?

There’s two issues with your code.

  1. You should be using parent = loader.item, not parent = loader. You don’t want the Loader to be your parent, but the item that it has loaded.
  2. 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 property using 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.

Leave a Comment

Your email address will not be published.

Scroll to Top