![](/img/trans.png)
[英]QML reports ReferenceError: XYZ is not defined on C++ object added to context
[英]QML Listview and c++: ReferenceError Model not defined
我想將我的C ++類顯示為QML中的ListView
。 我有一個空向量,稍后將填充:
QList<QObject*> _myList;
並設置上下文
QQmlContext *ctxt = _eng->rootContext();
ctxt->setContextProperty("modelName",QVariant::fromValue(_myList));
在qml文件中
ListView {
id: listView
model: modelName
delegate: myDelegate {}
}
但是啟動應用程序時出現以下錯誤
qrc:/screen2.qml:252: ReferenceError: modelName is not defined
我究竟做錯了什么? 遺憾的是,該錯誤不會阻止列表在填充后正確顯示。
加載QML文件之前,請調用setContextProperty
。
加載QML文件時,由於尚未設置modelName
的context屬性,因此引擎會評估其綁定,它會輸出警告。 當您隨后對其進行設置時,將重新評估此綁定,這就是最終在您的情況下正確顯示該列表的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.