Nulik
Nulik

Reputation: 7350

Why doesn't Loader sets parent's width and height?

Why, when I load a component with Loader, the width and height aren't copied form parent item ?

This code should show a red rectangle but it doesn't:

import QtQuick 2.7
import QtQuick.Controls 2.1
import QtQuick.Controls.Material 2.0
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.3

Pane {
    width: 400
    height: 300
    Item {
        anchors.fill: parent

        Loader {
            sourceComponent: redsq
        }
    }
    Component {
        id: redsq
        Rectangle {
            //width: 100;
            //height: 40;
            anchors.fill:  parent
            color: "red"
        }
    }
}

Now, if I uncomment the width and height and comment the anchors part, it will work fine:

Rectangle {
    width: 100;
    height: 40;
    //anchors.fill:  parent
    color: "red"
}

Output of bin/qmlscene:

Loader works correctly

So , what is the problem here, isn't it supposed that properties are propagating by property binding system? Or does Loader miss some features?

Upvotes: 2

Views: 1825

Answers (1)

dtech
dtech

Reputation: 49289

The loader is not being filled by the content by default, nor does it assume the content's size by default.

You can either bind the loader size to the content item size, or bind the content item size to the loader size.

Currently, your loader has zero width and height. So any items filling it will have zero size as well. Have another anchors.fill: parent for the loader, or just replace the Item with the Loader as the former is redundant.

Upvotes: 1

Related Questions