I would like to set a context property of a specific QML Component instead of in the root context. I do not want the property accessible outside of the component. Is there a way from C++ to access the context of the Component to only allow the named property to be accessible from within the component's context, and not from the global namespace? I would like to keep the QML declarative and to not create the component in C++ to access it's context.
//main.qml
Item {
Button {
// this should NOT work
text: ctxProp.text
}
OtherQml {
}
}
//OtherQml.qml
Item {
Button {
// this should work
text: ctxProp.text
}
}
//main.cpp
QGuiApplication app(art, argv);
QQmlQpplicationEngine engine;
// Some QObject Type
CtxProp ctxProp;
// I'd like to set the context such that only OtherQml.qml can access
// this context propery. Setting in root context property makes it global
engine.rootContext()->setContextProperty("ctxProp", &ctxProp);