Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-99287

ListView should have a property to size its delegate automatically

    XMLWordPrintable

    Details

    • Type: Suggestion
    • Status: Open
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Labels:
      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

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            Activity

              People

              Assignee:
              qt.team.quick.subscriptions Qt Quick and Widgets Team
              Reporter:
              grecko Pierre-Yves Siret
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:

                  Gerrit Reviews

                  There are no open Gerrit changes