Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

How to load a component from qml file in loader



  • Hi,

    I have component definition in a.qml. I want to load this component in main.qml.

    code snippet:

    a.qml:

    Component {
    id: testa
    Rectangle {
    width: 100
    height: 60
    color: 'red'
    }
    }

    main.qml:
    Loader {
    sourceComponent: testa
    }

    It's throwing error saying reference error. Tried with source:"a.qml". I am doing something wrong.

    Any idea?

    Thanks.


  • Moderators

    Hi,

    If you are using different file use source instead of sourceComponent and set it to QML file.



  • Tried with source:“a.qml”

    couldn't load component. If same component code available in main.qml, then it is able to load.

    Any idea.


  • Moderators

    Try removing the Component from a.qml. Just keep the Rectangle.


Log in to reply