代码之家  ›  专栏  ›  技术社区  ›  numberCruncher

QML Listview和c++:未定义ReferenceError模型

  •  3
  • numberCruncher  · 技术社区  · 7 年前

    我想将我的C++类显示为 ListView

    QList<QObject*> _myList;
    

    并设置上下文

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

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

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

    我做错了什么?令人窒息的是,该错误并不会阻止列表在填充后正确显示。

    1 回复  |  直到 7 年前
        1
  •  5
  •   GrecKo    7 年前

    呼叫 setContextProperty 在加载QML文件之前。

    modelName ,它输出警告。