Priority: P3: Somewhat important
Affects Version/s: None
Fix Version/s: None
Component/s: Quick: Core Declarative QML
Most of the time when using a ListView you want its delegates to have the same width than the ListView (or height for a horizontal ListView).
Users are tempted to reference the parent of the delegate to do that, but that leads to bugs: (some examples: https://codereview.qt-project.org/c/qt/qtconnectivity/+/377980 , https://stackoverflow.com/questions/70389271/typeerror-cannot-read-property-right-of-null )
This isn't very intuitive so their is a whole paragraph about it in the ListView documentation:
Delegates are instantiated as needed and may be destroyed at any time. As such, state should never be stored in a delegate. Delegates are usually parented to ListView's contentItem, but typically depending on whether it's visible in the view or not, the parent can change, and sometimes be null. Because of that, binding to the parent's properties from within the delegate is not recommended. If you want the delegate to fill out the width of the ListView, consider using one of the following approaches instead:
Some more context can be found here : https://bugreports.qt.io/browse/QTBUG-82989
What I propose here is that the sizing would be done by the ListView instead of the delegates:
It seems like a worthy property to add to avoid such a common (and not intuitive) boilerplate code.
Other advantages are that you sometime only added an id to the ListView so the delegates could reference it to access its width.
To have a correctly size delegate before you had to write
Neither could be sanely factored and hidden in Foo.qml. The first one because it would have to mention an id in another context which is a big no no. The second one because it assumes it would only be used in a ListView which might not be the case.
With this suggestion you can just do
without having to assume where it would be used in Foo.qml