Skip to content Skip to sidebar Skip to footer

How Do You Dynamically Load Multiple Components With QML?

I have a sample project at: https://github.com/jh3010-qt-questions/dynamic_loading I have read https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html which explains how

Solution 1:

One way to do it is to localize the scope of the functions/variables. You can nest functions within other functions so that they can still reference common variables, but subsequent calls to the outer function won't interfere.

function generateObjects() {
    function generateOneObject(name) {
        var component
        var componentObject

        function finishCreation() {
            componentObject = component.createObject( contentColumn );
        }

        component = Qt.createComponent(`qrc:/${name}`)

        if (component.status === Component.Ready) {
            finishCreation()
        } else {
            component.statusChanged.connect( finishCreation );
        }
    }

    for (var index in componentNames) {
        generateOneObject(componentNames[index])
    }
}

Component.onCompleted: {
    generateObjects()
}

Post a Comment for "How Do You Dynamically Load Multiple Components With QML?"