Object Id visibility and best practices
{QtQuick 2.0, Qt 5.1.0, Mingw}
If A.qml is a composition in B.qml which is a composition in main.qml then A.qml can see and refer to all object id’s in B.qml as well as main.qml of which it has no idea of if seen in isolation. Is doing so recommended as a good practice in qml world ?
eg.,
Code:
//A.qml
Rectangle {
//can see and use objRoot (see main.qml below) and one (see B.qml below)
}
//B.qml
Item {
One { //assume there's One.qml too
id: one
}
A {
}
}
//main.qml
Rectangle {
id: objRoot
B {
}
}
An eg. when this could be required: A.qml has a drag-able component. Then when it is dragged we would need to call ParentChange to make objRoot the parent for the duration of the drag so that it remains a top level item, visible no matter where it’s dragged around the window.
If this is not a good practice (because if later someone decides to give a similar object id in the composition chain then the one that is encountered 1st would be used breaking the code eg., if Someone included a composition of Some.qml in B.qml in addition to A.qml and One.qml above and decides to name its id as objRoot it would be perfectly legal but would silently break the code) then what’s the alternative ?
Re: Object Id visibility and best practices
If A needs an object to reparent something to then it should have a property for that and the code using it should set the appropriate object as the properties value.
Cheers,
_
Re: Object Id visibility and best practices
If it's deep inside composition-chain you would end up requiring a lot of property aliases all the way up