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
            y: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 ?


  • Moderators

    Hi! This works for me:

    main.qml

    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++)} );
                ready(globalId)
            }
        }
    }
    

    MyFile.qml

    import QtQuick 2.0
    
    Rectangle {
        width: 50
        height: 50
        color: "blue"
    
        function saySomething() {
            console.log("something")
        }
    }
    


  • @Wieland Wow.. Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.