Details
-
Suggestion
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
None
-
None
Description
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:
ListView { id: listView // ... delegate: Item { // Incorrect. width: parent.width // Correct. width: listView.width width: ListView.view.width // ... } }
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:
ListView {
sizeDelegatesToView: true
delegate: Item { }
}
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
delegate: Foo { width: listView.width }
or
delegate: Foo { width: ListView.view.width }
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
delegate: Foo {}
without having to assume where it would be used in Foo.qml
Attachments
Issue Links
- relates to
-
QTBUG-81976 ListView delegates produces "TypeError: Cannot read property 'width' of null" when removing item from QAbstractItemModel
- Closed
Gerrit Reviews
For Gerrit Dashboard: QTBUG-99287 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
514121,1 | WIP/RFC: ListView: Add delegatesResizeMode property | dev | qt/qtdeclarative | Status: NEW | -2 | 0 |