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

modelData is not defined: Broken view behavior with required property in delegate descendent

    XMLWordPrintable

Details

    Description

      Observe the snippet of code below. The model is simple (not based on QAbstractItemModel), can be a number or a list. Normally, such models' data are accessed through the "modelData" property, which is automatically created when the model is simple. In this case, "modelData" is undefined.

      Run the snippet with qmlscene and you get (yes, 5 times):

      ReferenceError: modelData is not defined
      ReferenceError: modelData is not defined
      ReferenceError: modelData is not defined
      ReferenceError: modelData is not defined
      ReferenceError: modelData is not defined
      

      If a descendent of the delegate, i.e. not the delegate itself, uses the "required" qualifier for a property, the view will automatically switch to the "modelData does not exist" mode, aka the "model roles are bound to required properties" mode. So what happens below, is that the presence of "required" in the MyComponent item, or any other of the delegate's descendents, which are typically completely unrelated to the model or the view at all, will cause the view to switch modes.

      This may be intentional, but I think it's a bug. If the idea is that required properties are meant to automatically bind to the model's roles, this behavior makes sense only at the top level of the delegate. In descendents, if this is intended behavior, it means any item used in a delegate that has a required property needs to be careful not to use a name for that property that matches any role of the model, which breaks code locality.

      import QtQuick 2.15
      
      Item {
          component MyComponent: Item {
              required property bool foo // remove required to fix
          }
      
          Repeater { // replace with ListView, same fault
              model: 5
      
              delegate: Item {
                  property var foo: modelData
      
                  MyComponent {
                      foo: true
                  }
              }
          }
      }
      

      Attachments

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

        Activity

          People

            qtqmlteam Qt Qml Team User
            andrhans Andreas Aardal Hanssen
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes