setContextProperty: Best practice for having the rootContext
maxwell31 last edited by
I use a QAbstractListItemModel which I want to have as a member of a class. In order to have it available in QML, I need to use
QQmlContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
However, I wonder what the best practice is for having the rootContext available in my class. Should I just give it as argument to the constructor? Or is it better to have a function which returns my model, so that I can set the context property in a place where I have the rootContext already (e.g. the main function or a gui handler function)?
Or does it make sense to make the rootContext a global variable?
It's not clear why you want to have access to the QML context from within your C++ class.
It sounds rather like a bad idea.
What does that class do except containing that model you mentioned ?