Please nominate your Qt Champions for 2021!

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:


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

    Loader {
    sourceComponent: testa

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

    Any idea?


  • Moderators


    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