Search code examples
javascriptruntimeqmlqt5

Dynamically create ListModel in QML


When I need to create any QML component in runtime, I can use that guide: http://qt-project.org/doc/qt-5/qtqml-javascript-dynamicobjectcreation.html

i.e. just call Qt.createComponent and component.createObject

But I couldn't find how to create ListModel at runtime? with qml, not in c++.

You can ask, why I need it. So, I have a nested ListModel: there is outer model, which delegates contained inner models. So when I'm calling outer_model.append({}), I must pass newly created ListModel for inner model. I cannot use statically defined inner model in outer delegate, because I cannot access such model in runtime. By the way, can it be accessed somehow?

P.S. Maybe it's completely wrong idea to try managing models in javascript?


Solution

  • Try this:

    Component {
        id: someComponent
        ListModel {
        }
    }
    
    function createModel(parent) {
        var newModel = someComponent.createObject(parent);
        return newModel;
    }