Important: Please read the Qt Code of Conduct -

How to insert loaded components from separate file into a Grid in QML?

  • i have a code here that loads component from separate qml file into main.qml.
    i created a grid to contain loaded component:

        Grid {
            id: scr
            width: 900
            height: parent.height
            x: 0

    the qml code to load component:

                var component = Qt.createComponent("qrc:/MyFile.qml");
                var object  = component.createObject(scr,{"objectName": "someID"});

    the component loads several times, i want it to arrange in grid, but it doesn't.

    Thanks in advance

  • Moderators

    @Alper The posted code looks fine. What's in MyFile.qml ?
    Do you get any errors ?

  • Hi! This works for me:


    import QtQuick 2.6
    import QtQuick.Window 2.2
    Window {
        id: mainWindow
        visible: true
        property int globalId: 0
        signal ready(int gid)
        Grid {
            id: grid
            anchors.fill: parent
        MouseArea {
            anchors.fill: parent
            onClicked: {
                var component = Qt.createComponent("qrc:///MyFile.qml");
                var object  = component.createObject(grid, {"objectName":"someIdentifier_"+(globalId++)} );


    import QtQuick 2.0
    Rectangle {
        width: 50
        height: 50
        color: "blue"
        function saySomething() {

  • @Wieland Wow.. Thanks!