qt5-cadaques
qt5-cadaques copied to clipboard
The warning paragraph is not very clear
The expression "a later loaded document can overwrite the element id's from an earlier loaded document" is confusing. It is not clear how code looks like that uses this "bad practice" but nevertheless the paragraph ends with "Please use this with care". To me as a reader (and somebody working on QML itself) it is not clear what "this" exactly is and how to avoid it therefore.
I would suggest to reformulate this to be independent of the time ("earlier loaded, later loaded") but instead perhaps utilize the term "nesting". For example
Button.qml: Item { width: outside.width }
Main.qml:
Rectangle { id: outside
Button {
}
}
The Button element is nested inside the scope of the Main qml file, so it can access objects by id from the outer scope, i.e. it can bind its width to the width of an outer element. However that breaks encapsulation, because it means the Button can only be used when the qml file that uses it contains an element called "outside".
However as soon as Button.qml would define an object with the id "outside", then any reference to "outside" within Button.qml will always find the one within the same file. There is no "global" variable or later/earlier loaded document that can overwrite that.
back-link: ch04/index.html#properties
Thanks for the comment. Makes much more sense. I wanted to say something similar, but seem I messed it up quit nicely.