2
votes

I want to display my C++ class as a ListView in QML. I have an empty vector, which will later be filled:

QList<QObject*> _myList;

and set the context

QQmlContext *ctxt = _eng->rootContext();
ctxt->setContextProperty("modelName",QVariant::fromValue(_myList));

In the qml file I have

ListView {
    id: listView
    model: modelName
    delegate: myDelegate {}
}

But when starting the application I get the following error

qrc:/screen2.qml:252: ReferenceError: modelName is not defined

What am I doing wrong? Strangly, the error does not prevent the list from being correctly displayed once it is filled.

1

1 Answers

3
votes

Call setContextProperty before loading your QML file.

When you load your QML file, the engine evaluates its bindings, since you haven't yet set the context property for modelName, it outputs a warning. When you set it afterwards, this binding is reevaluated and that's why the list is eventually correctly displayed in your case.