Luca
Luca

Reputation: 10996

Create a component as a member in an object instance

I have a QML component called MyComponent and I have an instance of it as follows:

MyComponent {
    id: rightComponent

    property Component settingsScreen: SettingsScreen {}

    StackView {
        id: settingsStack
        anchors.fill: parent
        initialItem: rightComponent.settingsScreen
    }
}

SettingsScreen is another component that I have. The problem is that it does not seem that rightComponent is the parent of settingsScreen. I get the correct result when I embed as:

MyComponent {
    id: rightComponent

    SettingsScreen {}
}

This is fine and everything is laid out correctly.

Upvotes: 2

Views: 47

Answers (1)

skypjack
skypjack

Reputation: 50540

You can solve as it follows:

MyComponent {
    id: rightComponent

    SettingsScreen {
        id: settingsScreen
    }

    StackView {
        id: settingsStack
        anchors.fill: parent
        initialItem: settingsScreen
    }
}

There is no need to define a new property as you did.

Upvotes: 2

Related Questions